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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With