Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

google api - googleapiclient in python, using updateContact returns 403

I'm trying to get a list of contacts from google's API, and then update some of them. While running the updateContact function I get 403 error Request person.etag is different than the current person.etag. Clear local cache and get the latest person." every time.
I'm reading the contact's etag using this code:

service = build('people', 'v1', credentials=creds)
results = service.people().connections().list(
        resourceName='people/me',
        pageSize=100,
        personFields='names,emailAddresses').execute()
connections = results.get('connections', [])

Then for each contact in the connections, Im trying yo change the contact givenName and familyName:

service.people().updateContact(resourceName="contact's resource name",
                              updatePersonFields="names",
                              body={
                                  "etag": "contact's etag",
                                  "names": [
                                    {
                                      "familyName": "new family name",
                                      "givenName": "new given name"
                                    }
                                  ]
                                }
                              ).execute()

The contact's etag and resourceName are taken from the connection's list response.
for example:

print(connections[0])
{'resourceName': 'people/<resource_number>',
 'etag': 'etag string',
 'names': [{'metadata': {'primary': True,
    'source': {'type': 'CONTACT', 'id': 'id number'}},
   'displayName': 'x',
   'familyName': 'x',
   'givenName': 'x',
   'displayNameLastFirst': 'x',
   'unstructuredName': 'x'}]}
question from:https://stackoverflow.com/questions/65842428/googleapiclient-in-python-using-updatecontact-returns-403

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Running your exact code does work.

I ran your code and based it on the Google People Python Quickstart, while changing only two things:

  1. The permissions

  2. Adding your code

    SCOPES = ['https://www.googleapis.com/auth/contacts']
    
    ...
    
    service = build('people', 'v1', credentials=creds)
    
    results = service.people().connections().list(
        resourceName='people/me',
        pageSize=5,
        personFields='names,emailAddresses').execute()
    connections = results.get('connections', [])
    
    for contact in connections:
        service.people().updateContact(resourceName=contact['resourceName'],
                                       updatePersonFields="names",
                                       body={
                                           "etag": contact['etag'],
                                           "names": [
                                               {
                                                   "familyName": "new family name",
                                                   "givenName": "new given name"
                                               }
                                           ]
                                       }
                                       ).execute()
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...