Čamo's avatar
Level 3

Socialite - how to get user with Javascript token

I want to login user via Google Javascript SDK which get short time token from Google and then need to send this token to Laravel 11 which shouuld connect to Google with the token and get user data. I would like to use socialite but dont see any documentation which would support this model. I dont want to redirect user anywhere but use JS SDK. Does Socialite have methods to connect Google with token? Thanks.

1 like
10 replies
Čamo's avatar
Level 3

I have a Vue application which login user with Google. Javascript get access_token and send this token to Laravel. What I expect is the Socialite should be able to login to Google API with this token and get user data and login user. I tried this

// $token is that one which Javascript send to Laravel
$user_google_data = Socialite::driver('google')->userFromToken($token);

But this throws me an error:

resulted in a `401 Unauthorized` response:
{
  "error": "invalid_request",
  "error_description": "Invalid Credentials"
}
 {"exception":"[object] (GuzzleHttp\\Exception\\ClientException(code: 401): Client error: `GET https://www.googleapis.com/oauth2/v3/userinfo?prettyPrint=false` resulted in a `401 Unauthorized` response:
{
  \"error\": \"invalid_request\",
  \"error_description\": \"Invalid Credentials\"
}
1 like
JussiMannisto's avatar

@vincent15000

You can use the userFromToken method only if the user is already authenticated.

If you have an access token, then the user is already authenticated.

1 like
vincent15000's avatar

@JussiMannisto Yes that's exactly what I said.

The userFromToken method is only to retrieve the authenticated user's informations when he is already authenticated, because you need to pass the token to the method.

Čamo's avatar
Level 3

@vincent15000 because you need to pass the token to the method??? Thats exactly what I am doing. And thats the reason why I ask the question.

1 like
vincent15000's avatar

@Čamo Hmmm ok ... perhaps I didn't understand what you need.

I thought you wanted to authenticate the user to the application via a google account.

Can you show how you retrieve the token please ?

JussiMannisto's avatar

@Čamo If you've configured everything correctly on Google Cloud and in config/services.php, and you have a valid access token, then that should work.

1 like
Čamo's avatar
Level 3

I changed the title.

1 like

Please or to participate in this conversation.