首页 > 解决方案 > 更新 Google 域共享联系人照片

问题描述

我正在使用 Google Apps 脚本访问域共享联系人并使用 People API 更新联系人信息。

我可以使用以下方法成功地将联系人照片添加到授权用户联系人:

var photoClass = {photoBytes: Utilities.base64EncodeWebSafe(testIDPhotoBlob.getBytes()),
personFields:'photos'};
var testId = 'people/c12345678901234'
var updatePhoto = People.People.updateContactPhoto(photoClass,testId);

这成功地更新了联系人照片。请注意,联系人是用户联系人,并且是带有“people/c”的存在。

如果我使用相同的方法更新域共享联系人:

var photoClass = {photoBytes: Utilities.base64EncodeWebSafe(testIDPhotoBlob.getBytes()),
personFields:'photos'};
var testId = 'people/d12345678901234'
var updatePhoto = People.People.updateContactPhoto(photoClass,testId);

请注意,联系人 ID 以“person/d”开头。这将返回错误:

GoogleJsonResponseException: API call to people.people.updateContactPhoto failed with error: Resource name "people/d12345678901234" is not a valid contact person resource.

域联系人确实存在为:

var testPersonSearch = People.People.searchDirectoryPeople({query:'Domain Contact Name',readMask:findFields,sources:['DIRECTORY_SOURCE_TYPE_DOMAIN_CONTACT']});

Returns:
{ people: 
   [ { organizations: [Object],
       names: [Object],
       phoneNumbers: [Object],
       emailAddresses: [Object],
       etag: '%xxxxxx/yyyyyyy',
       resourceName: 'people/d12345678901234"' } ],
  totalSize: 1 }

我了解旧 API /m8/feeds 没有正确实现此功能。

如果我对 SharedContactsAPI 使用以下代码:

var contact = SharedContactsApp.getContactById('hexadecimalValueofContact');
console.log(contact.hasProfilePicture()); 
returns false as contact is valid but no image exists....
console.log(contact.setProfilePicture(testIDPhotoBlob));
returns null

此外,通过直接使用 UrlFetchApp.fetch 等使用 REST API 会得到类似的结果,说明无法找到联系人。

标签: google-apps-scriptgoogle-apigoogle-people-api

解决方案


对于共享联系人,您需要使用共享联系人 API

头脑:

共享联系人 API 仅适用于外部联系人。使用此 API 为域用户或组创建联系信息可能会导致这些用户和组的联系信息重复,这可能会导致意外行为。

要实现Shared Contacts APIwith Apps 脚本,您可以使用开源SharedContactsApp库。该文档为您提供了如何使用它的说明和示例。


推荐阅读