Google+ APIs and Emails

I am trying to use the Google+ login API in ASP.NET with this code:

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 

As indicated, the Email field in info is null. If I add anything else to Scopes like "email" or " https://www.googleapis.com/auth/plus.profile.emails.read " ( link ) I get "invalid_scope"

Leaving this to people: get the API ( link ) to get emails no better:

 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 

What am I missing here?

+6
source share
1 answer

In the Scopes array, you need to add PlusService.Scope.UserinfoEmail to be able to view information related to the email.

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

In addition, you will need to declare an additional scope in the "data area" attribute of your login button. In this case it will be

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

Hope this helps.

EDIT: If you use https://github.com/googleplus/gplus-quickstart-csharp/ as a starting point, your data should look like this: http://pastebin.com/VJzhzyyk

+8
source

Source: https://habr.com/ru/post/971000/


All Articles