I'm trying Socialite with laravel 5.1 and I always got this error during call back route
InvalidStateException in AbstractProvider.php line 200:
and this is my code for callback route
public function handleProviderCallback($provider) {
$user = Socialite::driver($provider)->user();
}
additional information about issue
I saw this error happened when I authenticated from facebook only , when I disable this code in AbstractProvider.php line 200
throw new InvalidStateException;
Show new error
cURL error 7: Failed to connect to graph.facebook.com port 443
Related
I am building an API (my first api in this framework) in laravel 8 with passport 10 for accesses by token, to consult and test the API I use Insomnia, in insomnia I do the work environment with two variables "base" for the URL and "token".
When I test my API routes from Insomnia and pass the Token, it does not give me problems, the errors start when I remove the environment variable Token in Insomnia, the laravel API crashes with a 405 error (Method not Allowed).
My question is how can I control laravel so that when the user doesn't send the token my application doesn't crash?
I insist, this only happens when I remove the Token environment variable in Insomnia.
Thank you
First, i want to make sure that you are sending the Accept and Content-Type.
Accept: application/json
Content-Type: application/json
Second, MethodNotAllowedException indicates that you calling the route with different HTTP verb as you can see supported method is POST and you are using GET.
Third, in order to catch the MethodNotAllowedException you can add in your Handler.php located at app\Exceptions\Handler in report method
public function render($request, Throwable $e)
{
if ($request->ajax() && $e instanceof MethodNotAllowedException) {
return response()->json([
"Please enter your token"
], 405);
}
return parent::render($request, $e);
}
i have created a laravel socialte setup .and it was working before perfectly now its showing error(below).
1)i have changed client_secret
2)created a new oauth credentials
still not working
public function redirectToGoogle()
{
return Socialite::driver('google')->redirect();
}
public function handleGoogleCallback()
{
$user = Socialite::driver('google')->stateless()->user();
$user->getId(); // 1472352
$user->getNickname(); // "overtrue"
$name= $user->getName(); // "安正超"
$emailid= $user->getEmail();
$pic= $user->getAvatar(); // "anzhengchao#gmail.com"
return->redirect('welcome');
}
i have created env file with client_secret and client id
"""
Client error: `POST https://accounts.google.com/o/oauth2/token` resulted in a `400 Bad Request` response:\n
{\n
"error" : "invalid_grant",\n
"error_description" : "Code was already redeemed."\n
}\n
"""
When Google return the Authentication Code code to your Socialite, it can only be used to exchange to Access Token once. Doing more than once will result in the error Code was already redeemed.
The flow should be:
User click the login button on your website
You redirect user to Google and Google is asking user to login/grant you access
If successful, Google redirects back to you with a one-time-use Authentication Code?code=.....
Socialite use the ?code and exchange it with Google to get user's Access Token. This can only be done once per flow.
You can now request user details using the access token requested in step 4.
Read similar answer: https://stackoverflow.com/a/32710034/534862
Anyone knows why the route '/password/email' returns 404 Not Found error on production server, but works on local?
When user is trying to reset his password by email (https://compariimobiliare.ro/password/reset), the POST URL https://compariimobiliare.ro/password/email gives 404 Not Found error. Same URL works fine on local server, but not on production.
The route is there, I can see it when I run the command:
php artisan route:list
POST | password/email | | App\Http\Controllers\Auth\ForgotPasswordController#sendResetLinkEmail | web,guest
Laravel default implementation:
public function sendResetLinkEmail(Request $request)
{
$this->validate($request, ['email' => 'required|email']);
// We will send the password reset link to this user. Once we have attempted
// to send the link, we will examine the response then see the message we
// need to show to the user. Finally, we'll send out a proper response.
$response = $this->broker()->sendResetLink(
$request->only('email')
);
if ($response === Password::RESET_LINK_SENT) {
return back()->with('status', trans($response));
}
// If an error was returned by the password broker, we will get this message
// translated so we can notify a user of the problem. We'll redirect back
// to where the users came from so they can attempt this process again.
return back()->withErrors(
['email' => trans($response)]
);
}
I can see you have POST route for password/email.
Do you have a GET route for password/email as well?
You are getting 404 because it is trying to access GET route.
I have an web app which I bundled using webpack, I placed my entire react/redux app in the public file which will be served by nodejs(express-generator). My app works when I run in localhost/ local env. However when I deploy to heroku. I cannot make calls.
The below is the error message:
bundle.js:19 GET https://glacial-cove-64389.herokuapp.com/users/ 401 (Unauthorized)
Object {err: Error: Request failed with status code 401 at e.exports (https://glacial-cove-64389.herokuapp.co…}
err
:
Error: Request failed with status code 401 at e.exports (https://glacial-cove-64389.herokuapp.com/bundle.js:19:10382) at e.exports (https://glacial-cove-64389.herokuapp.com/bundle.js:26:6821) at XMLHttpRequest._.(anonymous function) (https://glacial-cove-64389.herokuapp.com/bundle.js:19:9464)
__proto__
:
Object
initially I thought it could be my my ROOT_URL so I changed it the below is an example of my actions file.
const ROOT_URL = "//glacial-cove-64389.herokuapp.com"
const axiosOption = {headers: { authorization : localStorage.getItem('token')}}
/*Sign in user*/
export function signinUser({ email, password }){
return function(dispatch){
axios.post(`${ROOT_URL}/users/signin`, { email, password })
.then(function(res){
dispatch({ type: AUTH_USER })
localStorage.setItem('token', res.data.token);
browserHistory.push('/users');
})
.catch(function(err){
dispatch(authError('Invalid email or password'))
console.log({err});
})
}
}
So what happens is that the react recognize the login and push user to the correct route. but it return the above error msg status code 401 once it hits the main pages.
The main problem I have is when I try to perform CRUD which doesn't work
Here is my repo: https://github.com/boyboi86/API_basic_random
I found out the hard way..
If you intend to put everything within your public file when scaffold with express-generator. Putting CORS within your Nodejs is insufficient because now your axios (react) that makes the call is also subjected to CORS, And you will have to config within your axios with the following:
axios.defaults.headers.post['Access-Control-Allow-Methods'] = 'PATCH, DELETE, POST, GET, OPTIONS';
This is to ensure all calls made will be allowed. I realised this when I look at the response headers.
I am trying to use Google PHP API to sream contents from Google plus. I was able to get the refresh token. But when I tried to get access token using that refresh token, its returning the following error.
Fatal error: Uncaught exception 'apiAuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'' in /var/www/social-media-test/google-api-php-client/src/auth/apiOAuth2.php:242 Stack trace: #0 /var/www/social-media-test/google-api-php-client/src/apiClient.php(281): apiOAuth2->refreshToken('MY-REFRESH-TOKEN-HERE') #1 /var/www/social-media-test/google-api-php-client/examples/plus/p.php(19): apiClient->refreshToken('MY-REFRESH-TOKEN-HERE') #2 {main} thrown in /var/www/social-media-test/google-api-php-client/src/auth/apiOAuth2.php on line 242
MY code is as shown below.
$client = new apiClient();
$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('client-id');
$client->setClientSecret('secret-key');
$client->setRedirectUri('http://localhost/index.php/main');
$client->setDeveloperKey('dev-key');
$plus = new apiPlusService($client);
$client->refreshToken('MY-REFRESH-TOKEN');
Any Ideas?
you forgot this:
$client->setScopes(array(
'https://www.googleapis.com/auth/blogger'
));
ref: https://developers.google.com/gdata/docs/auth/oauth#Scope