Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Google api to use for getting user's first name, last name, picture, etc?

I have the oauth authorization with google working correctly and am getting data from the contacts api. Now, I want to programmatically get a gmail user's first name, last name and picture. Which google api can i use to get this data?

like image 320
Pranav Avatar asked Jan 21 '10 10:01

Pranav


People also ask

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.

What is userId in Gmail API?

userId. string. The user's email address. The special value me can be used to indicate the authenticated user.


2 Answers

The contacts API perhaps works, but you have to request permission from the user to access all contacts. If I were a user, that would make me wary of granting the permission (because this essentially gives you permission to spam all my contacts...)

I found the response here to be useful, and only asks for "basic profile information":

Get user info via Google API

I have successfully used this approach, and can confirm it returns the following Json object:

{
  "id": "..."
  "email": "...",
  "verified_email": true,
  "name": "....",
  "given_name": "...",
  "family_name": "...",
  "link": "...",
  "picture": "...",
  "gender": "male",
  "locale": "en"
}
like image 196
Amit Dubey Avatar answered Sep 22 '22 03:09

Amit Dubey


Use this code to get firstName and lastName of a google user:

final HttpTransport transport = new NetHttpTransport();
final JsonFactory jsonFactory = new JacksonFactory();
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
        .setAudience(Arrays.asList(clientId))
        .setIssuer("https://accounts.google.com")
        .build();

GoogleIdToken idToken = null;
try {
    idToken = verifier.verify(googleAuthenticationPostResponse.getId_token());
} catch (GeneralSecurityException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
GoogleIdToken.Payload payload = null;
if (idToken != null) {
    payload = idToken.getPayload();
}
String firstName = payload.get("given_name").toString();
String lastName = payload.get("family_name").toString();
like image 21
Mohammadreza Tavakoli Avatar answered Sep 21 '22 03:09

Mohammadreza Tavakoli