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

岁酱吖の 提交于 2019-12-25 02:28:54

问题


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.



来源:https://stackoverflow.com/questions/24408578/how-to-get-email-id-from-oauth-2-0-using-google-apis-auth-oauth2-flows-googleaut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!