如何使用 Google Apps 脚本获取 Gmail 用户的个人资料图片?

How to get a Gmail user's profile picture using Google Apps Script?

我想在我们的域工作区中更新用户的电子邮件签名。我想使用用户的个人资料图片作为电子邮件签名的一部分。但是,我无法在电子邮件签名中呈现它。

查询完目标用户后,我目前所做的如下:

  1. 已使用 AdminDirectory.Users.Photos.get(email_address) 函数获取个人资料照片属性。使用这些属性创建编码图像 URL 以在 HTML img src 属性中显示它。所以,<img src="data:${userPhoto.mimeType};base64,${userPhoto.photoData}">。但是没有头像显示。

  2. 使用了 getThumbnailPhotoUrl() 函数,但它呈现默认占位符图像,即使有个人资料图片集也是如此。

我还添加了范围:'https://www.googleapis.com/auth/admin.directory.user' 用于更新域中目标用户的服务凭据。

还有其他方法吗?

users.photos.get 您应该 return 做出以下回应:

{
  "kind": "admin#directory#user#photo",
  "id": "112573616946026931368",
  "etag": "\"-vDsmNkFadksL3sdO9_llNHf95VVWFrsahpYPMFRU6U/X6ZsLw-qrvYjOu4FN-GLlBCL-ts\"",
  "primaryEmail": "EMAIL",
  "mimeType": "image/jpeg",
  "height": 96,
  "width": 96,
  "photoData": "photoDataString"
}

其中photoData定义如下:

string (bytes format)

The user photo's upload data in web-safe Base64 format in bytes. This means:

The slash (/) character is replaced with the underscore (_) character. The plus sign (+) character is replaced with the hyphen (-) character. The equals sign (=) character is replaced with the asterisk (*). For padding, the period (.) character is used instead of the RFC-4648 baseURL definition which uses the equals sign (=) for padding. This is done to simplify URL-parsing. Whatever the size of the photo being uploaded, the API downsizes it to 96x96 pixels. A base64-encoded string.

现在,您可以将照片 blob 检索到例如按照以下方式将其存储在您的云端硬盘上:

function saveUserPhoto() {
  var data = "photoDataString"
  var bytes = Utilities.base64DecodeWebSafe(data)
  var blob = Utilities.newBlob(bytes, "image/png", "userPhoto");
  DriveApp.createFile(blob);
}