How to get Email ID from oAuth 2.0 using Google.Ap

2019-09-01 02:44发布

问题:

My Question is basically How to get Email ID from oAuth 2.0 using Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow ?

I am using MVC 4 application with GoogleAuthorizationCodeFlow for oAuth 2.0 implementation. After the page goes to the google and the user enters Email ID and password.

How could i Get the Email ID from the API?

回答1:

There are many oauth scopes that give you the email.
Search for them in the oauth2 playground. The newest one is the google+ scope ised for 'login with g+' and google recommends using that scope instead of the older ones.



回答2:

I think your answer is found here. Link to the page is at the bottom of this post.

"The first step is to call #loadCredential(String) based on the known user ID to check if the end-user's credentials are already known. If not, call newAuthorizationUrl() and direct the end-user's browser to an authorization page. The web browser will then redirect to the redirect URL with a "code" query parameter which can then be used to request an access token using newTokenRequest(String). Finally, use #createAndStoreCredential(TokenResponse, String) to store and obtain a credential for accessing protected resources."

http://javadoc.google-api-java-client.googlecode.com/hg-history/0dd03fb10eb0ec8e66259d5a19e712bb274b68bc/1.10.0-beta/jdiff/Google%20API%20Client%20Library%20for%20Java%201.9.0-beta/com/google/api/client/googleapis/auth/oauth2/GoogleAuthorizationCodeFlow.html



回答3:

After authorization in googleapi. You will get acees_token,refresh_token etc along with id_token. The id_token is a string contains email field. But it is base64 encoded format. So you have to decode the id_token string using base64_decode. The id_token is coantains 2 stirng appended by '.'. The scond string contains the meat data of email,cilent id etc.