Search code examples
apigoogle-apigoogle-plusgoogle-contacts-apigoogle-signin

Fetching Google Plus profile url and email


If I open someone's Google Plus Profile Page I see contact info and information shared on Google Plus. I looking for similar information on Google API. I'm trying to fetch list of user's contacts with email and google plus profile id, that's all.

Here I can fetch user connections with Google Plus profile url but without email or phone number.

https://people.googleapis.com/v1/people/me/connections

Here I can fetch person contacts with email and phone number (OAuth2) - without Google Plus profile url nor id

https://www.google.com/m8/feeds/contacts/{GOOGLE_ACCOUNT_NAME}%40gmail.com/full?alt=json

But I don't know how to combine this two outputs, to get have Google Plus profile url and contact information.


Solution

  • You are correct. To retrieve profile information for a user, use the people.get API method. To get profile information for the currently authorized user, use the userId value of me.

    gapi.client.load('plus','v1', function(){
    var request = gapi.client.plus.people.get({
    'userId': 'me'
    });
    request.execute(function(resp) {
    console.log('Retrieved profile for:' + resp.displayName);
    });
    });
    

    Note that this method requires authentication using a token that has been granted the OAuth scope https://www.googleapis.com/auth/plus.login or https://www.googleapis.com/auth/plus.me.

    Plus.People.List listPeople = plus.people().list(
    "me", "visible");
    listPeople.setMaxResults(5L);
    
    PeopleFeed peopleFeed = listPeople.execute();
    List<Person> people = peopleFeed.getItems();
    
    // Loop through until we arrive at an empty page
    while (people != null) {
    for (Person person : people) {
    System.out.println(person.getDisplayName());
    }
    
    // We will know we are on the last page when the next page token is
    // null.
    // If this is the case, break.
    if (peopleFeed.getNextPageToken() == null) {
    break;
    }
    
    // Prepare the next page of results
    listPeople.setPageToken(peopleFeed.getNextPageToken());
    
    // Execute and process the next page request
    peopleFeed = listPeople.execute();
    people = peopleFeed.getItems();
    }
    

    Here's a related SO ticket which discuss how to fetch user email from Google+ Oauth: How to get user email from google plus oauth