[英]how to get email after using google OAuth2 in C#?
我使用代码获取凭据
static string[] Scopes = { "https://www.googleapis.com/auth/userinfo.email" };
private static UserCredential GenerateCredential()
{
UserCredential credential;
using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
return credential;
}
如何从此凭证中获取 email? 我试过代码
private string GetEmailFromCredentials(UserCredential credential)
{
var plusService = new PlusService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "My Application",
});
var me = plusService.People.Get("me").Execute();
var useremail = me.Emails.FirstOrDefault().Value;
return useremail;
}
但看起来 People.Get("me") 不再可行了。 我收到错误消息“Google.Apis.Requests.RequestError Legacy People API 之前未在项目 618254727025 中使用或已被禁用”
您可以使用users.getprofile它将返回当前经过身份验证的用户的 email 地址。
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "My Application",
});
var user = service.Users.GetProfile("me").Execute;
{
"emailAddress": "xxxx1@gmail.com",
"messagesTotal": 8394,
"threadsTotal": 1494,
"historyId": "605503"
}
people.get的正确用法是“people/me”
var request = plusService.People.Get("people/me")
request.PersonFields("emailAddresses");
var response = request.Execute();
在您的范围变量中。 尝试使用值“email”而不是完整的 https 地址。 web 链接中的 Scope 关键字以空格分隔。 这是我获取范围的一种方法:profile email openid。
要测试这种方法,您可以在获取访问代码后手动将以下链接粘贴到浏览器中: https://www.googleapis.com/oauth2/v2/userinfo?access_token=[PASTE ACCESS CODE HERE]&[PASTE VALUE FROM下面的变量授权请求在这里]
仅供参考:我正在修改可用的演示代码: https://github.com/googlesamples/oauth-apps-for-windows 。
// Creates the OAuth 2.0 authorization request. string authorizationRequest = string.Format("{0}?response_type=code&scope=openid%20profile%20email&redirect_uri={1}&client_id={2}&state={3}&code_challenge={4}&code_challenge_method={5}", authorizationEndpoint, System.Uri.EscapeDataString(redirectURI), clientID, state, code_challenge, code_challenge_method);
我已经在这个问题上停留了几天。 最后,感谢这个链接( 检查用户是否已经登录),我了解到参数输入“用户”是关键问题。 这个“用户”应该是 windows 登录用户(可以使用Enviroment.Username
),而不是程序员或APP用户。 GoogleWebAuthorizationBroker.AuthorizeAsync
使用此用户名将其凭据保存在以下位置:
C:\Users[用户名]\AppData\Roaming\Google.Apis.Auth\Google.Apis.Auth.OAuth2.Responses.TokenResponse-[用户名]
(像这样)。 因此,如果您将“用户”提供给AuthorizeAsync
,则凭证保存可能会出现问题,并且您的应用程序将严重挂起或滞后。 而且,以后想用cred文件获取userinfo和email的时候,会出现问题(严重滞后)。 在我的情况下,用户信息将全部丢失,只留下一个 email 地址。 此外,您必须包含所需的两个范围:“https://www.googleapis.com/auth/userinfo.email”、“https://www.googleapis.com/auth/userinfo.profile”。 希望这些有所帮助。
oauth2Client
。id_token
,它基本上是一个 jwt 令牌,解析它你会得到email
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.