0

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

phomlish
  • 189
  • 1
  • 2
  • 13
  • Can you show us the response given by Google (HTTP Status) ? – Cylexx Jul 26 '17 at 08:55
  • switching to HttpResponse r = oauth2.userinfo().v2().me().get().executeUnparsed(); I see 200, "OK" – phomlish Jul 26 '17 at 09:30
  • Your response contains "emails" like this ? `"kind": "plus#person", "etag": "\"rthtrsgd\"", "gender": "male", "emails": [ { "value": "28@gmail.com", "type": "account" } ], "objectType": "person", "id": "`XXXXXXXXXXXXXXXXX",` – Cylexx Jul 26 '17 at 09:42
  • updated question to show the toString output which does not show nulls. Inspecting in the debugger also shows email is null. – phomlish Jul 26 '17 at 09:54
  • I think it miss you the scope https://www.googleapis.com/auth/userinfo.email – Cylexx Jul 26 '17 at 09:59
  • according to this post, that scope is deprecated: https://stackoverflow.com/questions/24442668/google-oauth-api-to-get-users-email-address – phomlish Jul 26 '17 at 10:05
  • But no choice if you really want emails – Cylexx Jul 26 '17 at 11:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150186/discussion-between-phomlish-and-cylexx). – phomlish Jul 26 '17 at 11:39
  • turned out my scopes were not being read correctly. I trimmed it down to JUST email scope and I now receive the primary email. – phomlish Aug 22 '17 at 13:16

1 Answers1

1

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();
phomlish
  • 189
  • 1
  • 2
  • 13