2017-04-13 6 views
0

사용자간에 서명을 표준화하기 위해 내 도메인의 사용자 서명에 액세스하는 데 도움이되는 아래 코드를 작성했습니다. 현재 URLFetch 매개 변수에 'PATCH'메소드를 지정하면 이전 서명을 포함하여 보낸 전자 메일의 sendAs 리소스 만받습니다. PUT 메소드를 지정하면 서명을 제거하지만 지정한 서명을 계정에 설정하지 않습니다. 누군가 내가 내가 뭘 잘못하고 있는지 보도록 도와 줄 수 있을까?GMAIL 서명을 도메인 유효성 검사로 업데이트하는 REST 요청

////////////////////////////////////////////////////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

흥미롭게도, 고급 서비스 구글 스크립트에서 패치 기능은 상기 페이로드와 함께 작동합니다. 당신이 고급 서비스를 사용할 수 없을 수도 있다는 것을 알고 있습니다, 이것은 단지 FYI입니다. 희망이 다른 사람이 알아낼 수 있습니다! –

답변

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) 
     };