Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google+ SignIn API and emails

I am trying to use the Google+ Sign-in API in ASP.NET using 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 stated the Email field at info is null. If I add anything else to the Scopes like "email" or "https://www.googleapis.com/auth/plus.profile.emails.read" (reference) I get "invalid_scope"

Leaving it to people:get API (reference) to get the emails is not any 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?

like image 427
el_shayan Avatar asked Jun 18 '14 11:06

el_shayan


People also ask

Is Gmail email API free?

Gmail API is available for free, but it has certain daily usage limits for API calls. Daily usage: 1 billion API calls per day. Per User Rate Limit: 250 API calls per user per second.

What data can I get from Google login?

After you have signed in a user with Google using the default scopes, you can access the user's Google ID, name, profile URL, and email address.


1 Answers

In the Scopes array you'll need to add PlusService.Scope.UserinfoEmail in order to be able to view the email-related information.

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

Also, you will need to declare the additional scope in the "data-scope" attribute of your sign in button. In this case, it would be

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

Hope this helps.

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

like image 143
EvilBeer Avatar answered Sep 17 '22 23:09

EvilBeer