2017-04-13 63 views
0

我创建了下面的代码,它提供了一些帮助来访问我的域上的任何用户签名,以便在用户之间实现标准化。目前,当我在URLFetch参数中指定方法'PATCH'时,我得到的只是我发送的电子邮件的sendAs资源,包括旧签名。如果我指定了PUT方法,它将删除签名,但不会将我指定的签名设置到该帐户上。有人能帮我看看我做错了什么吗?REST请求,通过域验证更新GMAIL签名

////////////////////////////////////////////////////FUNCTION SET SIGNATURE//////////////////////////////////////////////////////////////////////// 
 

 
/** 
 
* Authorizes and makes a request to the GMail API. 
 
*/ 
 
function setSignature(user) 
 
{ 
 
    var user = '[email protected]'; 
 
    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(); 
 
}

注:OAuth2用户凭据都存储在一个单独的文件中恒变量,但我已验证凭据返回的有效数据。

谢谢,

+0

有趣的是,先进的服务谷歌脚本补丁功能的工作原理与上面提到的有效载荷。我知道你可能无法使用高级服务,这仅仅是供参考。希望其他人能弄清楚这一点! –

回答

0

我的应用程序的作品,问题是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) 
     };