How to get user id after login with google oauth [

2019-01-20 11:11发布

This question already has an answer here:

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.

3条回答
\"骚年 ilove
2楼-- · 2019-01-20 11:46

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
查看更多
何必那么认真
3楼-- · 2019-01-20 11:58

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

查看更多
Evening l夕情丶
4楼-- · 2019-01-20 12:01

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?

查看更多
登录 后发表回答