Reputation: 274
I'm using the Google People API in Python to query my Google Contacts list. I can get this feature to work, but one specific operation does not work.
results = service.people().connections().list(
resourceName='people/me',
pageSize=1,
personFields='names,phoneNumbers').execute()
connections = results.get('connections', [])
for person in connections:
data = person.get('names', [])
This works -- it gives me a list of names. However, I want a list of names and phone numbers. If I do this:
for person in connections:
data = person.get('phoneNumbers', [])
This also works. I get a list of phone numbers. However, if I try to get both, I get no results:
for person in connections:
data = person.get('names,phoneNumbers', [])
According to my interpretation of the People API docs (https://developers.google.com/people/api/rest/v1/people/get), this should work. Instead, I just get nothing.
What am I doing wrong?
Upvotes: 0
Views: 1515
Reputation: 2442
As @marmor has said, the error lies in how you are accessing the content of the response. The method dict.get()
does refuse two keys at the same time.
Here are two different approaches that you can use.
# Calling them separately
personName = person.get('names', [])
personPhone = person.get('phoneNumbers',[])
# Calling them together using list compression
keys = ["names","phoneNumbers"]
data = [person.get(key) for key in keys]
You should refer to people().connections().list() to better understand the response format. In this case, it is a resource of type Person
As another possible approximation is, getting a list of names and their phone numbers and store them in a dictionary:
def getNameAndPhones():
service = people_service()
results = service.people().connections().list(
resourceName= "people/me",
personFields='names,phoneNumbers').execute()
person_dict = {}
for per in results['connections']:
try:
name = per['names'][0]['displayName']
# As the response is an array
# we need to do some list comprehension
phoneNumbers = [x['value'] for x in per['phoneNumbers']]
person_dict[name] = phoneNumber
except :
name = per['names'][0]['displayName']
person_dict[name] = "No phone Number"
print(person_dict)
return person_dict
{
"Eco":["00112233"],
"Maxim maxim":"No phone Number",
"dummy super":[
"123123123",
"12312309812390"
]
}
Upvotes: 1
Reputation: 28179
When you call person.get()
you are not calling the v1/people/get
endpoint, you're just getting a value from a dictionary.
The previous call to service.people().connections()....execute()
already gets all the information from the API and puts it in a list of dictionaries.
Then when you iterate over connections
you get a dictionary named person
which holds two keys names
and phoneNumbers
.
Your code is trying to get a key with the name of names,phoneNumbers
which doesn't exist.
Not sure how you're using data
or what type are you expecting it to be, but to have data
contain both name and phones simply do:
data = person
or just use person
directly in the rest of your code.
Upvotes: 1