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.
Asked
Active
Viewed 2.3k times
6
Huy Tran
- 4,371
- 10
- 34
- 38
-
1Please try printing the response from oauth once the authorization is done.. this will give you an idea of where to get user id from. It should be either inside "user_id" tag in response or inside "raw" tag. – balanv Sep 30 '12 at 15:15
-
This is redirect link after login with oauth `http://localhost:8888/mbproject/user/oauth2callback?code=4/NWpMSgUcu9rXTlmWzZc0Zwj_BWYu.kotuJPCNkBYQgrKXntQAax186_w0dAI` , It's not includes user_id in GET parameter, just contain `code` – Huy Tran Oct 01 '12 at 04:03
-
I actually use rails.. and i can see the result dump in `request.env["omniauth.auth"]` try this in your server code.. actually what's your backend server script? – balanv Oct 01 '12 at 05:44
-
Thanks balanv, it gives in user email in auth para result – Hardik Darji May 06 '16 at 09:14
3 Answers
6
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
Jan Gerlinger
- 7,361
- 1
- 44
- 52
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
Jeff Posnick
- 53,580
- 14
- 141
- 167
-
Is it possible to list all YouTube channels per specific YouTube user if you are not that user? – prosti May 18 '21 at 22:11
2
You can do one of the following:
- perform a GET on
www.googleapis.com/oauth2/v1/userinfo - decode the
id_tokenthat 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?