我创建了以下代码,并提供了一些帮助,可以访问我域中的任何用户签名,以便跨用户标准化签名。目前,当我指定方法' PATCH'在URLFetch参数中,我得到的是我发送的电子邮件的sendAs资源,包括旧签名。如果我指定PUT方法,它会删除签名,但不会将我指定的签名设置到帐户中。有人能帮助我看看我做错了吗?
////////////////////////////////////////////////////FUNCTION SET SIGNATURE////////////////////////////////////////////////////////////////////////
/**
* Authorizes and makes a request to the GMail API.
*/
function setSignature(user)
{
var user = 'me@mydomain.com';
var newSig = '<b>This is my new Signature!</b>';
var service = getService(user);
if (service.hasAccess()) {
var url = 'https://www.googleapis.com/gmail/v1/users/'+user+'/settings/sendAs/'+user;
var payload =
{
"sendAsEmail" : user,
"displayName" : AdminDirectory.Users.get(user).name.fullName,
"type" : "patch",
"replyToAddress" : user,
"signature": newSig
};
var options =
{
"method" : "PUT",
"payload" : payload,
"muteHttpExceptions": true,
"contentType": "ctAPPLICATION_JSON",
"headers": {Authorization: 'Bearer ' + service.getAccessToken()}
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
} else {
Logger.log(service.getLastError());
}
}
////////////////////////////////////////////////////FUNCTION VIEW SIGNATURE////////////////////////////////////////////////////////////////////////
function viewSignature(user) { var user = USER_EMAIL;
var service = getService(user);
Logger.log(service.hasAccess());
if (service.hasAccess()) {
var url = 'https://www.googleapis.com/gmail/v1/users/'+user+'/settings/sendAs';
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
}
});
var result = JSON.parse(response.getContentText());
Logger.log(JSON.stringify(result, null, 2));
} else {
Logger.log(service.getLastError());
}
}
////////////////////////////////////////////////////FUNCTION RESET//////////////////////////////////////////////////////////////////////////////////////
/**
* Reset the authorization state, so that it can be re-tested.
*/
function reset() {
var service = getService();
service.reset();
}
///////////////////////////////////////////////////////////FUNCTION GET SERVICE////////////////////////////////////////////////////////////////////////
/**
* Configures the service.
*/
function getService(user) {
return OAuth2.createService('Gmail:' + user)
// Set the endpoint URL.
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
// Set the private key and issuer.
.setPrivateKey(PRIVATE_KEY)
.setIssuer(CLIENT_EMAIL)
// Set the name of the user to impersonate. This will only work for
// Google Apps for Work/EDU accounts whose admin has setup domain-wide
// delegation:
// https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
.setSubject(user)
// 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://www.googleapis.com/auth/gmail.settings.basic', 'https://www.googleapis.com/auth/gmail.settings.sharing');
}
////////////////////////////////////////////////////FUNCTION CLEAR SIGNATURE////////////////////////////////////////////////////////////////////////
function clearService(){
OAuth2.createService('drive')
.setPropertyStore(PropertiesService.getUserProperties())
.reset();
}
&#13;
注意:OAuth2 Credentials存储在Constant变量中的单独文件中,但我已验证凭据是否返回有效数据。
谢谢,
答案 0 :(得分:1)
我的应用有效,问题是contentType。
尝试:
var formData = {'sendAsEmail':user,'type':'patch','signature': newSig}
var options =
{
'method' : 'put',
'muteHttpExceptions': true,
'contentType': 'application/json',
'headers': {Authorization: 'Bearer ' + service.getAccessToken()},
'payload': JSON.stringify(formData)
};