phomlish
phomlish

Reputation: 189

Google OAuth2 API v2 userinfo always returns null for email

I'm trying to get the authenticated user's email addresses.

I've done the authentication with scopes

email
profile
https://www.googleapis.com/auth/plus.login
https://www.googleapis.com/plus/v1/people/me
https://www.googleapis.com/auth/userinfo.email

This call returns null for email but name, link, picture, etc are populated:

Userinfoplus userinfo2 = oauth2.userinfo().v2().me().get().execute();
log.info(userinfo2.toString());

outputs:

{
  "family_name" : "Homlish",
  "gender" : "male",
  "given_name" : "Paul",
  "id" : "107004799409225320539",
  "link" : "https://plus.google.com/107004799409225320539",
  "locale" : "en",
  "name" : "Paul Homlish",
  "picture" : "https://lh4.googleusercontent.com/-bCRlXUqr__E/AAAAAAAAAAI/AAAAAAAABR8/LQCliyz_jgI/photo.jpg"
} 

inspecting I can see an email field, but it is null.

Any ideas what I am missing?

Although the scopes I am using don't prompt the user for extra permissions, are there any scopes I can delete as not needed?

Thanks, Paul

Upvotes: 0

Views: 2659

Answers (1)

phomlish
phomlish

Reputation: 189

By setting scope to 'email', authenticating, and using com.google.api.client.googleapis.auth.oauth2.GoogleCredential and com.google.api.services.oauth2.Oauth2 I was able to receive email:

        GoogleCredential credential = new GoogleCredential().setAccessToken(tokenFromAuthentication); 
        Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
                .setApplicationName("Oauth2")
                .build();
        Userinfoplus userinfo = oauth2.userinfo().get().execute();

Upvotes: 1

Related Questions