[英]How to get user's email address using google Oauth api
I am using with Google's OAuth 2.0 Playground using my own personal Google account, but I cannot seem to recover my Gmail address using the playground.我正在使用我自己的个人 Google 帐户使用 Google 的 OAuth 2.0 Playground,但我似乎无法使用 Playground 恢复我的 Gmail 地址。 I am using the api url https://www.googleapis.com/oauth2/v3/userinfo .
我正在使用 api url https://www.googleapis.com/oauth2/v3/userinfo 。 But didn't get email address.
但是没有得到 email 地址。 I get various information about the user such as family name, first name, gender, picture, etc. I have referred with Google OAuth API to get user's email address?
我得到了有关用户的各种信息,例如姓氏、名字、性别、照片等。我参考了Google OAuth API 以获取用户的 email 地址? but it didn't work.
但它没有用。 I am using the code(c#-windows form) below(to get email address & user information)
我正在使用下面的代码(c#-windows 表单)(获取 email 地址和用户信息)
string userinfoRequestURI = "https://www.googleapis.com/oauth2/v3/userinfo
HttpWebRequest userinfoRequest = (HttpWebRequest)WebRequest.Create(userinfoRequestURI);
userinfoRequest.Method = "GET";
userinfoRequest.Headers.Add(string.Format("Authorization: Bearer {0}", access_token));
userinfoRequest.ContentType = "application/x-www-form-urlencoded";
userinfoRequest.Accept = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
WebResponse userinfoResponse = await userinfoRequest.GetResponseAsync();
//userinfoResponse.
using (StreamReader userinfoResponseReader = new StreamReader(userinfoResponse.GetResponseStream()))
{
// reads response body
string userinfoResponseText = await userinfoResponseReader.ReadToEndAsync();
output(userinfoResponseText);
}
When you authorized your request over on oauth playground, make sure you sent email.当您在 oauth 操场上授权您的请求时,请确保您发送了 email。
If you did send the email scope then i recommend going though the People api rather then the userinfo endpoint, but remember to add the profile scope and the email scopes If you did send the email scope then i recommend going though the People api rather then the userinfo endpoint, but remember to add the profile scope and the email scopes
The userinfo endpoint is not guaranteed to return the claims every time.不保证 userinfo 端点每次都返回声明。 Its better to go though the People api for that.
最好是 go 尽管人们 api 为此。
Tip: consider looking into the google api .net client library.提示:考虑查看 google api .net 客户端库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.