7

I want to retrieve my contacts names, email address and phone numbers using the Google People API and I'm using their Try it! tool to test the API.

The names are being retrieved but not the emails addresses and phone numbers. I think I'm doing everything correctly. I'm properly authenticated with the proper scopes and selected fields. enter image description here enter image description here

Am I doing something wrong? Or maybe this is an issue on Google's side?

nunoarruda
  • 2,679
  • 5
  • 26
  • 50

5 Answers5

17

I have found a solution. According to the Google People API docs omitting this RequestMask field will include all fields but that's not happening. In my case setting the RequestMask field to person.names,person.emailAddresses,person.phoneNumbers works.

Community
  • 1
  • 1
nunoarruda
  • 2,679
  • 5
  • 26
  • 50
  • Excellent find. Thank you for making it possible for me to stop banging my head into the People API wall <3 – Schuuure Jul 12 '16 at 18:30
  • You saved me with this! Thank you! – Felipe Francisco Nov 14 '16 at 15:34
  • This was giving me Forest Whitaker eye. Thank you. – Joel Mar 10 '17 at 21:09
  • Hey, I've being trying to use the google people api with your provided request. But I'm have this error0 { "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See developers.google.com/identity/sign-in/web/…;, "status": "UNAUTHENTICATED" } } My request URL is https://people.googleapis.com/v1/people/me/connections?key=api_key Can you help me out with what the problem here is – Parth Vyas Mar 11 '17 at 19:51
  • 1
    The documentation says `RequestMask` is deprecated, use `personFields` instead https://developers.google.com/people/api/rest/v1/RequestMask – Yvonne Aburrow Nov 08 '17 at 11:12
2

You have to use people.connections.list to fetch the list of contacts, then loop over them and use people.get in order to fetch each individual contact. In my case I set pageSize to 50 and then I use people:batchGet (which supports up to 50 resource names at a time) to fetch the details for those 50 contacts.

2

Just to be clear — because it really wasn't to me — if you are constructing the GET request yourself the query parameter for the request mask is requestMask.includeField. The nested JSON is flattened to a keypath. So the complete connection list GET request would be something like:

https://content-people.googleapis.com/v1/people/me/connections?requestMask.includeField=person.names,person.addresses,person.email_addresses,person.organizations,person.phone_numbers

sumizome
  • 712
  • 8
  • 11
2

User's phone numbers and other details will be fetched only if the user has added them to their Google profile.

If you are using Try this API tool and with 'resourceName = people/me', then :

  1. you should be logged in with your Google account and
  2. give your consent to allow the google API explorer tool to access logged in user's (in this case your) profile details. See example here.

After the consent is given by you, it will be able to fetch 'phoneNumbers' and 'addresses' for the logged in user.

If you want to access other user's profile or you are calling the API from any other source then:

  1. the user must give permission to your app/website/program to access the requested information from his/her profile

  2. you must include 'authorization' KEY in GET REQUEST HEADERS with VALUE "Bearer (OAuth accesstoken of the target user)" to get personal details from the target user's profile.

The OAuth accesstoken can be obtained directly from the user consent response or by exchanging refreshtoken for accesstoken.

Vismay Patel
  • 173
  • 1
  • 10
  • The OP specifically requested information about their contacts email addresses and phone number. You solution is for logged in user profile details – Kermit_ice_tea Jan 26 '18 at 23:21
1

Once you have credentials object saved somewhere. You can retrieve the phone numbers and other details by using following code. You just have to set the RequestMask

#get the credentials

cred_obj = OAuth2Credentials.new_from_json(credentials)
http = httplib2.Http()

#create service

people_service = build(serviceName='people', version='v1', http=cred_obj.authorize(http))

results = people_service.people().connections().list(resourceName='people/me',
                requestMask_includeField='person.names,person.emailAddresses,person.phoneNumbers',
                                                     pageSize=160)

res = results.execute()
connections = res.get('connections', [])

for person in connections:
    names = person.get('names', [])
    phone_numbers = person.get('phoneNumbers', [])
    if len(names) > 0:
        name = names[0].get('displayName')
    if len(phone_numbers)>0:
        phone_no = phone_numbers[0].get('value')
        print name, phone_no
rusty
  • 652
  • 7
  • 21