Google+ SignIn API和电子邮件

el_*_*yan 6 c# google-plus google-api-dotnet-client

我正在尝试使用此代码在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
Run Code Online (Sandbox Code Playgroud)

如上所述,该Email字段info为空.如果我在Scopes"email"或" https://www.googleapis.com/auth/plus.profile.emails.read "(参考)中添加其他内容,我会收到"invalid_scope"

留给人们:获取API(参考)来获取电子邮件并不是更好:

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
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?

Evi*_*eer 8

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

Scopes = new string[] { PlusService.Scope.PlusLogin,  PlusService.Scope.UserinfoEmail }
Run Code Online (Sandbox Code Playgroud)

此外,您还需要在登录按钮的"数据范围"属性中声明其他范围.在这种情况下,它会

data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email"
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

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