You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Google People API为其他Google Workspace用户管理联系人及头像?

Google Workspace People API 跨用户联系人管理权限及头像更新问题解答

1. 为何无法通过People API为其他用户创建联系人?

管理员确实具备通过People API读写其他用户联系人的权限,但必须启用域范围授权(Domain-Wide Delegation)并模拟目标用户身份,而非使用普通OAuth授权:

  • GAM能实现该操作,是因为它默认配置了域范围授权,直接以目标用户的身份发起API请求
  • 普通People API调用若未指定模拟用户,只能操作当前授权账户的联系人。要跨用户操作,需:
    1. 在Google Workspace Admin控制台为你的服务账号启用域范围授权
    2. 调用API时添加subject参数(值为目标用户的邮箱),模拟该用户身份执行操作

2. 更新其他用户联系人头像失败的原因及解决方法

报错GoogleJsonResponseException: Requested entity was not found的核心原因是未模拟目标用户身份:

  • 你用管理员身份直接调用API时,系统只会在管理员自己的联系人列表中查找该资源ID,而目标用户的联系人ID仅在其自身的联系人空间内有效,因此找不到对应实体
  • 修正方案:
    1. 配置域范围授权,获取目标用户的授权令牌
    2. 在Apps Script中模拟目标用户身份调用API,示例代码如下:
function updateOtherUserContactPhoto(targetEmail, contactResourceId, fileId) {
  // 使用OAuth2库获取目标用户的令牌(需先安装OAuth2库)
  var service = OAuth2.createService('PeopleAPI')
    .setTokenUrl('https://oauth2.googleapis.com/token')
    .setPrivateKey(PropertiesService.getScriptProperties().getProperty('PRIVATE_KEY'))
    .setIssuer(PropertiesService.getScriptProperties().getProperty('CLIENT_EMAIL'))
    .setSubject(targetEmail)
    .setPropertyStore(PropertiesService.getScriptProperties())
    .setScope('https://www.googleapis.com/auth/contacts');

  if (!service.hasAccess()) {
    throw new Error('无法获取目标用户授权: ' + service.getLastError());
  }

  var blob = DriveApp.getFileById(fileId).getBlob();
  var data = Utilities.base64EncodeWebSafe(blob.getBytes());
  var request = { photoBytes: data };
  
  // 用目标用户的令牌发起请求
  var options = {
    method: 'PATCH',
    headers: { Authorization: 'Bearer ' + service.getAccessToken() },
    contentType: 'application/json',
    payload: JSON.stringify(request)
  };
  
  UrlFetchApp.fetch(`https://people.googleapis.com/v1/${contactResourceId}:updateContactPhoto`, options);
}
  • 额外注意:需确保服务账号已被授予https://www.googleapis.com/auth/contacts权限,且目标用户的联系人资源ID完全正确

内容的提问来源于stack exchange,提问作者Jack Test

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 08:55:26