gpt4 book ai didi

c# - Google+ 登录 API 和电子邮件

转载 作者:太空狗 更新时间:2023-10-29 21:22:24 24 4
gpt4 key购买 nike

我正在尝试使用以下代码在 ASP.NET 中使用 Google+ 登录 API:

var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = secrets,
Scopes = new string[] { PlusService.Scope.PlusLogin }
});

token = flow.ExchangeCodeForTokenAsync("me", code, "postmessage", CancellationToken.None).Result;

var service = new Oauth2Service(new Google.Apis.Services.BaseClientService.Initializer());
var request = service.Tokeninfo();
request.AccessToken = token.AccessToken;
var info = request.Execute();
// info.Email is null here

如前所述,info 中的 Email 字段为空。如果我向 Scopes 添加任何其他内容,例如“email”或“https://www.googleapis.com/auth/plus.profile.emails.read”(reference),我会得到“invalid_scope”

将它留给 people:get API ( reference ) 来获取电子邮件也好不到哪里去:

var credential = new UserCredential(flow, info.UserId, token);
plusService = new PlusService(new Google.Apis.Services.BaseClientService.Initializer()
{
ApplicationName = "Test",
HttpClientInitializer = credential
});

var me = plusService.People.Get("me").Execute();
// me.Emails is null

我在这里错过了什么?

最佳答案

在 Scopes 数组中,您需要添加 PlusService.Scope.UserinfoEmail 以便能够查看与电子邮件相关的信息。

Scopes = new string[] { PlusService.Scope.PlusLogin,  PlusService.Scope.UserinfoEmail }

此外,您还需要在登录按钮的“数据范围”属性中声明附加范围。在这种情况下,它将是

data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email"

希望这对您有所帮助。

编辑:如果您使用的是 https://github.com/googleplus/gplus-quickstart-csharp/作为起点,您的数据应如下所示: http://pastebin.com/VJzhzyyk

关于c# - Google+ 登录 API 和电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24283902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com