【发布时间】:2021-06-09 08:21:39
【问题描述】:
我尝试使用 people api (python) 更新电子邮件地址。这是我的代码。请建议我将电子邮件地址更新为现有联系人的正确格式。
service = discovery.build('people', 'v1', http=http, discoveryServiceUrl='https://people.googleapis.com/$discovery/rest:updateContact?updatePersonFields=emailAddresses')
service.people().updateContact(
resourceName=profileCode,
body={"resourceName": profileCode, "etag": accessToken},
updatePersonFields='test@gmail.com',
).execute()
这是我的代码,请纠正我。
FLOW = OAuth2WebServerFlow(
client_id='1*****.apps.googleusercontent.com',
client_secret='Secret',
scope='https://www.googleapis.com/auth/contacts',
user_agent='contact_cms/YOUR_APPLICATION_VERSION')
def get_credentials(FLOW):
storage = Storage('info.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
credentials = tools.run_flow(FLOW, storage)
return credentials
def get_http(credentials):
http = httplib2.Http()
http = credentials.authorize(http)
return http
def update_a_google_contact(http):
profileCode = 'people/c3022811234561887053'
etag = '%EgU3AQI9abcdefMczZ1V0tVV1hubW89'
service = discovery.build('people', 'v1', http=http, discoveryServiceUrl='https://people.googleapis.com/$discovery/rest')
service.people().updateContact(
resourceName=profileCode,
body={"resourceName": profileCode, "etag": etag, "updatePersonFields=emailAddresses": [{ "value": "test@gmail.com" }]}
).execute()
def main():
creds=get_credentials(FLOW)
http=get_http(creds)
update_a_google_contact(http)
if __name__ == '__main__':
main()
【问题讨论】:
-
您为什么使用
http和discoveryServiceUrl而不是credentials?您没有从 GCP 项目下载的credentials文件?另外,为什么要在etag中指定您的accessToken?另外请提供http相关的代码。 -
嗨,谢谢。请找到代码。如果有错误请帮我纠正。
-
你是怎么想出这个代码 sn-p 的?为什么不使用常规流程,如the quickstart sample所示?
-
我是这个 API 的新手。现在我已将代码更改为新代码并找到了解决方案。非常感谢您的支持。
-
嗨!出于文档目的,您能否发布一个答案来解释您为解决此问题所做的更改?
标签: google-api google-people-api