Gmail API 调用返回空白且没有错误,但未应用对委托创建的更改
Gmail API call returning blank with no errors, but not applying change for delegate creation
我正在尝试在 App 脚本中调用 Gmail API,同时使用 Google's Oauth2 library for App Script 模拟用户帐户来为其收件箱设置委托。使用 Google 的示例作为基础,我创建了以下函数来调用服务并提供正确的信息以在收件箱上创建新的委托:
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());
}
}
此函数 returns 无任何内容 - 没有错误消息,但未创建委托。根据 API,我应该得到 delegates resource 的回报,但我没有。我做错了什么?
这是定义 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"
}
我正在尝试在 App 脚本中调用 Gmail API,同时使用 Google's Oauth2 library for App Script 模拟用户帐户来为其收件箱设置委托。使用 Google 的示例作为基础,我创建了以下函数来调用服务并提供正确的信息以在收件箱上创建新的委托:
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());
}
}
此函数 returns 无任何内容 - 没有错误消息,但未创建委托。根据 API,我应该得到 delegates resource 的回报,但我没有。我做错了什么?
这是定义 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"
}