如何用Google Apps Script通过People API获取域外用户邮箱(依据唯一ID)
如何通过Google Apps Script获取域外用户的邮箱地址?
可以实现,但需要调整People API的调用方式,并注意权限和用户隐私限制,具体方法如下:
1. 确认启用People API
在Google Apps Script编辑器中,点击扩展 > 高级Google服务,找到「People API」并开启;同时要在关联的Google Cloud Console中确认该API已启用(部分高级服务需要云控制台同步配置)。
2. 调整People API调用参数
你之前的调用只返回基础信息,是因为未指定需要获取的字段。必须在请求中添加personFields参数,明确要求获取emailAddresses字段。示例代码:
function getExternalUserEmail(peopleId) { try { const person = People.People.get(peopleId, { personFields: 'emailAddresses' }); if (person.emailAddresses && person.emailAddresses.length > 0) { return person.emailAddresses[0].value; } else { return "无法获取邮箱:权限不足或用户未公开邮箱信息"; } } catch (e) { return `调用失败:${e.message}`; } }
3. 权限与隐私限制说明
- 域外用户的邮箱能否获取,取决于对方的隐私设置,以及你的账号权限:如果对方未公开邮箱,或你的域与对方无协作关系,可能无法拿到有效信息。
- 若你是域管理员,可通过域管理工具查看协作用户的邮箱,但普通用户无此权限。
4. 替代方案(当People API失效时)
如果无法通过People API获取,可尝试从Drive权限记录中提取:若该用户是某Drive文件的共享者,调用Drive API的权限接口可能拿到邮箱。示例代码:
function getEmailFromDrivePermission(fileId, userId) { try { const permission = Drive.Permissions.get(fileId, userId); return permission.emailAddress; } catch (e) { return `无法获取:${e.message}`; } }
注意:此方法需要你拥有目标文件的访问权限,且用户的权限记录中包含邮箱字段。
内容的提问来源于stack exchange,提问作者iroic
相关产品推荐
相关产品推荐

