Gmail API调用返回空白,没有错误,但不应用更改来创建代理



我正在尝试在应用程序脚本中调用Gmail API,同时使用Google的应用程序脚本Oauth2库模拟用户帐户,为他们的收件箱设置代理。以谷歌的例子为基础,我创建了以下函数来调用服务,并提供正确的信息来在收件箱中创建新的代表:

function setGmailDelegate() {
var formObject = {
delegateReaderEmail: "homer.simpson@fox.com",
delegateTargetEmail: "robert.terwilliger@fox.com"
};
var userEmail = formObject.delegateReaderEmail;
var boxEmail = formObject.delegateTargetEmail;
Logger.log("Letting "+userEmail + " read " + boxEmail);
var service = getGmailAddDelegateService_(boxEmail);
if (service.hasAccess()) {
var url = 'https://www.googleapis.com/gmail/v1/users/' + boxEmail +'/settings/delegates';
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
body: {
"delegateEmail": userEmail,
"verificationStatus": "accepted"
}
});
var result = JSON.parse(response.getContentText());
Logger.log(result);
} else {
Logger.log(service.getLastError());
}
}

此函数不返回任何东西-没有错误消息,但未创建委托。根据API,我应该得到一个代理资源,但我没有。我做错了什么?

这是定义OAuth服务创建的函数:

function getGmailAddDelegateService_(boxEmail) { 
return OAuth2.createService('Gmail:' + boxEmail)
// Set the endpoint URL.
.setTokenUrl('https://oauth2.googleapis.com/token')
// Set the private key and issuer.
.setPrivateKey(PRIVATE_KEY)
.setIssuer(CLIENT_EMAIL)
// Set the name of the user to impersonate.
.setSubject(boxEmail)
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getScriptProperties())
// Set the scope. This must match one of the scopes configured during the
// setup of domain-wide delegation.
.setScope(['https://mail.google.com/','https://www.googleapis.com/auth/gmail.settings.sharing']);
}

我试过改变范围,但没有用。我在G Suite审计日志的执行日志中看不到任何指向任何问题的内容。

找到答案:URLApp需要指定您正在张贴数据:

var response = UrlFetchApp.fetch(url, {
method: 'post',
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
body: {
"delegateEmail": userEmail,
"verificationStatus": "accepted"
}