问题描述
我正在尝试使用 Google 应用 gs 中的人员服务更新现有联系人。我有一个这样的联系人:
let contactInfo = {
"name" : "Jhon","lastName": "Doe","phone": "+1999999","rn": "people/c8289118840931811524","etag": "%EgkBAj0JPgs/Ny4aBAECBQciDDV1TzVFdXY0ckhnPQ=="
}
我创建了以下功能以便更轻松地更新联系人:
function updateContact(contactInfo){
//var updatePersonFields = "updatePersonFields=names,phoneNumbers,emailAddresses";
var bodyRequest = {
"resourceName": contactInfo.rn,"etag": contactInfo.etag,"names": [{
"givenname": contactInfo.name,"familyName": contactInfo.lastName,}],"phoneNumbers": [{
'value': contactInfo.phone
}],"emailAddresses": [{
'value': contactInfo.email
}]
};
People.People.updateContact(bodyRequest,contactInfo.rn);
}
但是,updateContact 的文档指南需要 3 个参数:路径参数、查询参数和请求正文: https://developers.google.com/people/api/rest/v1/people/updateContact
为了更新联系人,我必须传递 updatePersonfields,但它是一个查询参数,而 updateContact 只接收 2 个参数。
我知道需要 etag 和 updatePersonfields,正如链接解释的那样: https://developers.google.com/people/v1/contacts?hl=en#update_an_existing_contact
如何添加查询参数 updatePersonFields(在评论中)?
如果 updatePersonField 没有发送,我有以下错误:
GoogleJsonResponseException: API call to people.people.updateContact Failed with error:
updatePersonFields mask is required. Please specify one or more valid paths.
Valid paths are documented at
https://developers.google.com/people/api/rest/v1/people/updateContact.
先谢谢你。
解决方法
在您的情况下,请将 updatePersonFields
作为对象包含在 People.People.updateContact
的第三个参数中。
在这种情况下,当您通过 Google Apps Script 的脚本编辑器使用 Advanced Google services 的 People API 时,您可以通过脚本编辑器的自动完成功能看到 updateContact(resource: Peopleapi_v1.Peopleapi.V1.Schema.Person,resourceName: string,optionalArgs: Object)
的文档。
所以,当你的脚本被修改后,就变成了这样。
来自:
People.People.updateContact(bodyRequest,contactInfo.rn);
致:
People.People.updateContact(bodyRequest,contactInfo.rn,{updatePersonFields: "emailAddresses,names,phoneNumbers"});
注意:
- 当您的
bodyRequest
为无效值时,会发生错误。请注意这一点。