如何使用 Google Apps 脚本设置或更新联系人的照片?

How to set or update a contact's photo with Google Apps Script?

我以this other question为例,看起来好像可以,但联系人的照片实际上并没有改变,也没有返回错误。

我能够获取联系人的当前照片并删除联系人的照片就好了。

我的代码

const accessToken = ScriptApp.getOAuthToken();
const id = '4c18faa28828aa3f';
const url = '-URL Omitted-';
const blob = UrlFetchApp.fetch(url).getBlob();
const data = Utilities.base64EncodeWebSafe(blob.getBytes());
const response = UrlFetchApp.fetch(`https://www.google.com/m8/feeds/photos/media/me/${id}`, {
  method: 'put',
  contentType: 'image/jpeg',
  payload: data,
  headers: {
    Authorization: `Bearer ${accessToken}`,
  },
});
const content = response.getContentText();
console.log(content);

回应XML

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom'>
  <id>http://www.google.com/m8/feeds/photos/media/-OMITTED-%40gmail.com/4c18faa28828aa3f</id>
  <updated>2020-10-12T22:10:01.271Z</updated>
  <link rel='self' type='application/atom+xml'
        href='https://www.google.com/m8/feeds/photos/media/-OMITTED-%40gmail.com/4c18faa28828aa3f'/>
  <link rel='edit' type='application/atom+xml'
        href='https://www.google.com/m8/feeds/photos/media/-OMITTED-%40gmail.com/4c18faa28828aa3f/1B2M2Y8AsgTpgAmY7PhCfg'/>
</entry>

我明白了。出于某种原因,我正在为有效负载编码图像 blob 字节。我应该像这样将 blob 作为有效载荷传递。

const accessToken = ScriptApp.getOAuthToken();
const id = '4c18faa28828aa3f';
const url = '-URL Omitted-';
const blob = UrlFetchApp.fetch(url).getBlob();
const response = UrlFetchApp.fetch(`https://www.google.com/m8/feeds/photos/media/me/${id}`, {
  method: 'put',
  contentType: 'image/jpeg',
  payload: blob,
  headers: {
    Authorization: `Bearer ${accessToken}`,
  },
});
const content = response.getContentText();
console.log(content);