要求Google在OAuth中返回电子邮件地址

Kha*_*ash 28 oauth dotnetopenauth

我使用OAuth通过dotNetOAuth访问Gmail.在授权后,我如何强制Google返回用户的电子邮件地址作为回调的一部分?

默认情况下,Google OAuth回调仅返回令牌密钥和访问令牌.

小智 65

首先,您需要将以下范围(https://www.googleapis.com/auth/userinfo.email)添加到您的oauth请求中.

从Google返回您的应用并获得访问令牌后,您可以使用访问令牌发出请求https://www.googleapis.com/userinfo/email?alt=json.这将返回电子邮件地址.有关详细信息,请访问http://sites.google.com/site/oauthgoog/Home/emaildisplayscope

  • 此链接可以帮助任何努力签署Google API请求的人(您需要添加授权标头):http://code.google.com/apis/accounts/docs/OAuth_ref.html#SigningOAuth (2认同)

And*_*ott 9

OAuth在OAuth握手期间不提供额外参数的工具,因此我认为您不能强制Google提供它.但是,有可能是Google API,您可以使用OAuth访问令牌在握手后调用以获取电子邮件地址.


Nar*_*ren 7

For getting the Email Id, you need to add the scope "https://wwww.googleapis.com/auth/userinfo.email"

Then you will get id_token in the response.

Response={
   "access_token" : "ya29.eAG__HY8KahJZN9VmangoliaV-Jn7hLtestkeys",
   "token_type" : "Bearer",
   "expires_in" : 3600,
   "id_token" : "id_token_from_server",
   "refresh_token" : "1/GIHTAdMo6zLVKCqNbA"
 }

Then use this id_token as below POST request:

https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=id_token_from_server

And you will get response like below:

Response={
 "issuer": "accounts.google.com",
 "issued_to": "80780.apps.googleusercontent.com",
 "audience": "8078909.apps.googleusercontent.com",
 "user_id": "1118976557884",
 "expires_in": 3598,
 "issued_at": 1456353,
 "email": "emailId@gmail.com",
 "email_verified": true
}

Make sure you add "www" in the APIs as shown above...
Run Code Online (Sandbox Code Playgroud)