我正在使用OAuth通过dotNetOAuth访问Gmail。如何在授权后强制Google返回用户的电子邮件地址作为回调的一部分?
默认情况下,Google OAuth回调仅返回令牌密钥和访问令牌。
答案 0 :(得分: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
答案 1 :(得分:9)
OAuth在OAuth握手期间不提供额外参数的工具,因此我认为您不能强制Google提供它。但是,有可能是Google API,您可以使用OAuth访问令牌在握手后调用以获取电子邮件地址。
答案 2 :(得分:6)
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...
答案 3 :(得分:2)
请求OAuth范围包含“电子邮件显示范围”https://www.googleapis.com/auth/userinfo.email
scope="http://www.google.com/m8/feeds/ https://www.googleapis.com/auth/userinfo.email"
然后使用像Hammock这样的REST API来获取地址
RestClient client = new RestClient
{
Authority = "https://www.googleapis.com",
};
RestRequest request = new RestRequest
{
Path = "userinfo/email?alt=json",
Credentials = OAuthCredentials.ForProtectedResource(
this.requestSettings.ConsumerKey,
this.requestSettings.ConsumerSecret,
this.requestSettings.Token,
this.requestSettings.TokenSecret)
};
var response = client.Request(request);
答案 4 :(得分:2)
这是一个c#函数,用于预先授权请求,如上所述:
private void FetchUsersEmail(token)
{
var emailRequest = @"https://www.googleapis.com/userinfo/email?alt=json&access_token=" + token;
// Create a request for the URL.
var request = WebRequest.Create(emailRequest);
// Get the response.
var response = (HttpWebResponse) request.GetResponse();
// Get the stream containing content returned by the server.
var dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
var reader = new StreamReader(dataStream);
// Read the content.
var jsonString = reader.ReadToEnd();
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
dynamic json = JValue.Parse(jsonString);
var currentGoogleEmail = json.data.email;
}
(JValue
是JSON.Net)
答案 5 :(得分:1)
在php中,apiOauth2Service.php类提供了访问登录用户信息的方法。为此,您可以使用userinfo-> get()方法。确保您还使用范围https://www.googleapis.com/auth/userinfo.email。
这将使用相同的访问令牌。此外,您应该尝试在其他API中查找类似的信息作为回报。通过oAuth_playground>>可以更容易地查看http://code.google.com/apis/explorer/
答案 6 :(得分:0)
如果您请求userinfo.email范围,Google会返回id_token以及access_token。
id_token可以不加密以提供用户的电子邮件地址,请访问www.googleapis.com?/oauth2/v1/tokeninfo?id_token=IDTOKENHERE
此处提供更多信息:https://developers.google.com/accounts/docs/OAuth2Login