加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Google SignIn API和电子邮件

发布时间:2020-12-15 08:17:16 所属栏目:百科 来源:网络整理
导读:我正在尝试使用此代码在ASP.NET中使用Google登录API: var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer{ ClientSecrets = secrets,Scopes = new string[] { PlusService.Scope.PlusLogin }});token = flow.Exchang
我正在尝试使用此代码在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的电子邮件字段为空.如果我在“电子邮件”或“https://www.googleapis.com/auth/plus.profile.emails.read”(reference)中添加任何其他内容,我会收到“invalid_scope”

留给人们:获取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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读