How to get user id after login with google oauth [

2019-01-20 11:25发布

问题:

This question already has an answer here:

  • How to identify a Google OAuth2 user? 6 answers

I use Google oAuth to get user info via YoutubeAPI, but I don't know what I need to do after get access_token to retrieve userID, anybody please help me.

回答1:

See Google's OAuth2 documentation for how to get information about the logged in user.

It's basically just a GET call to https://www.googleapis.com/oauth2/v1/userinfo with a correct access token. In the response the user id is included.

Note that you also need to include the correct scope in your very first redirect to Google:

https://www.googleapis.com/auth/userinfo.profile


回答2:

If you're asking about how to get either the YouTube username or the YouTube user ID for the currently authenticated user, it can be found in the response to a properly authenticated request to

http://gdata.youtube.com/feeds/api/users/default?v=2



回答3:

You can do one of the following:

  1. perform a GET on www.googleapis.com/oauth2/v1/userinfo
  2. decode the id_token that you get in the initial request, using a JWT library

I'd recommend the latter as it is more robust and doesn't require an extra call.

See this thread for more info: How to identify a Google OAuth2 user?