2017-04-24 84 views
0

我敢肯定有一些簡單的,我在這裏失蹤,但我已經在這一個多星期&不能得到它想通了,所以我問。我會以我不是一個真正的程序員的方式來解釋我的問題!我是需要使用Google Apps腳本爲我們的G Suite網域中的所有用戶配置簽名的網絡/系統管理員。我已經有了一點bash /命令行/ PowerShell的經驗,但是當涉及到「真正的」編程語言時,我幾乎什麼都不知道。使用谷歌Apps腳本設置域用戶的電子郵件簽名

話雖這麼說,我在這裏How to use the Gmail API, OAuth2 for Apps Script, and Domain-Wide Delegation to set email signatures for users in a G Suite domain讀另一SO頁有關如何設置電子郵件簽名。當我第一次嘗試腳本時,我根本無法完成它。我已經修改了它&設法得到它現在進行認證,但沒有任何反應,當它到達的地方,應設置簽名的一部分,它只是似乎退出&這就是它!這裏是我的修改後的代碼減去任何私人位:

// Adapted from script at https://stackoverflow.com/questions/40936257/how-to-use-the-gmail-api-oauth2-for-apps-script-and-domain-wide-delegation-to 

// these two things are included in the .JSON file that you download when creating the service account and service account key 
var OAUTH2_SERVICE_ACCOUNT_PRIVATE_KEY = '-----BEGIN PRIVATE KEY-----\n_MY_KEY_GOES_HERE_\n-----END PRIVATE KEY-----\n'; 
var OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL = '[email protected]erviceaccount.com'; 

function setSignatureTest() { 
    var email = '[email protected]'; 
    var signature = 'my test signature'; 
    var test = setSignature(email, signature); 
    Logger.log('test result: ' + test); 
} 

function setSignature(email, signature) { 
    Logger.log('starting setSignature'); 
    var signatureSetSuccessfully = false; 
    var service = getDomainWideDelegationService('Gmail: ', 'https://www.googleapis.com/auth/gmail.settings.basic', OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL); 
    if (!service.hasAccess()) { 
    Logger.log('failed to authenticate as user ' + OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL); 
    Logger.log(service.getLastError()); 
    signatureSetSuccessfully = service.getLastError(); 
    return signatureSetSuccessfully; 
    } else Logger.log('successfully authenticated as user ' + OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL); 
    var resource = { 'sendAsEmail' : email, 'userId' : OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL, 'signature' : signature }; 
    var options = 
     { 
     'method' : 'put', 
     'contentType' : 'application/json', 
     'Authorization' : 'Bearer ' + service.getAccessToken(), 
     'payload' : resource 
     }; 
    var emailForUrl = encodeURIComponent(email); 
    var url = 'https://www.googleapis.com/gmail/v1/users/me/settings/sendAs/' + emailForUrl; 
    var maxSetSignatureAttempts  = 1; 
    var currentSetSignatureAttempts = 0; 
    do { 
    try { 
     currentSetSignatureAttempts++; 
     Logger.log('currentSetSignatureAttempts: ' + currentSetSignatureAttempts); 
     var setSignatureResponse = UrlFetchApp.fetch(url, JSON.stringify(options)); 
     Logger.log('setSignatureResponse on successful attempt:' + setSignatureResponse); 
     signatureSetSuccessfully = true; 
     break; 
    } catch(e) { 
     Logger.log('set signature failed attempt, waiting 3 seconds and re-trying'); 
     Utilities.sleep(3000); 
    } 
    if (currentSetSignatureAttempts >= maxSetSignatureAttempts) { 
     Logger.log('exceeded ' + maxSetSignatureAttempts + ' set signature attempts, deleting user and ending script'); 
     Logger.log('URL: ' + url); 
     Logger.log('Value of JSON.stringify(options):' + JSON.stringify(options)); 
     Logger.log('Value of setSignatureResponse:' + setSignatureResponse); 
     throw new Error('Something went wrong when setting their email signature.'); 
    } 
    } while (!signatureSetSuccessfully); 
    return signatureSetSuccessfully; 
} 

function getDomainWideDelegationService(serviceName, scope, OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL) { 
    Logger.log('starting getDomainWideDelegationService for email: ' + OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL); 
    return OAuth2.createService(serviceName + OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL) 
     // Set the endpoint URL. 
     .setTokenUrl('https://accounts.google.com/o/oauth2/token') 
     // Set the private key and issuer. 
     .setPrivateKey(OAUTH2_SERVICE_ACCOUNT_PRIVATE_KEY) 
     .setIssuer(OAUTH2_SERVICE_ACCOUNT_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(OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL) 
     // 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(scope); 
} 

任何人有任何想法嗎?我確定有人在&之前做過這個,我在某個地方犯了一個簡單的錯誤。我覺得問題出在我的有效載荷選項上,但我真的不知道如何解決這個問題,我所嘗試的一切都不做任何事情。

編輯:請參閱下面的消毒日誌輸出。進行更改後2017.04.25.16:00

[17-04-24 18:24:27:087 PDT] starting setSignature 
[17-04-24 18:24:27:088 PDT] starting getDomainWideDelegationService for email: [email protected]serviceaccount.com 
[17-04-24 18:24:27:521 PDT] successfully authenticated as user [email protected]serviceaccount.com 
[17-04-24 18:24:27:550 PDT] currentSetSignatureAttempts: 1 
[17-04-24 18:24:27:552 PDT] set signature failed attempt, waiting 3 seconds and re-trying 
[17-04-24 18:24:30:554 PDT] exceeded 1 set signature attempts, deleting user and ending script 
[17-04-24 18:24:30:554 PDT] URL: https://www.googleapis.com/gmail/v1/users/me/settings/sendAs/user%40domain.com 
[17-04-24 18:24:30:555 PDT] Value of JSON.stringify(options):{"method":"put","contentType":"application/json","Authorization":"Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx」,」payload":{"sendAsEmail」:」[email protected]」,」userId":"[email protected]serviceaccount.com","signature":"my test signature"}} 
[17-04-24 18:24:30:556 PDT] Value of setSignatureResponse:undefined 

日誌輸出:

[17-04-25 12:37:00:260 PDT] starting setSignature 
[17-04-25 12:37:00:261 PDT] starting getDomainWideDelegationService for email: [email protected]serviceaccount.com 
[17-04-25 12:37:00:278 PDT] successfully authenticated as user [email protected]serviceaccount.com 
[17-04-25 12:37:00:289 PDT] currentSetSignatureAttempts: 1 
[17-04-25 12:37:00:343 PDT] setSignatureResponse on successful attempt:{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "failedPrecondition", 
    "message": "Bad Request" 
    } 
    ], 
    "code": 400, 
    "message": "Bad Request" 
} 
} 

[17-04-25 12:37:00:343 PDT] test result: true 
+1

什麼是您的日誌或執行成績單說呢? (查看>日誌/執行腳本) –

+0

請參閱我剛剛編輯的編輯,謝謝傑克! – user7916051

+0

您需要將此更改爲有效ID'變種的電子郵件=「[email protected]」;'也許你的ID,以確保它改變了你的簽名 –

回答

0

授權需要進入請求頭,像這樣

var options = 
     { 
     'headers' : {'Authorization' : 'Bearer ' + service.getAccessToken()}, 
     'method' : 'put', 
     'contentType' : 'application/json', 
     'payload' : JSON.stringify(resource) 
     }; 

最後你的API調用會看起來像這樣:

var setSignatureResponse = UrlFetchApp.fetch(url, options); 

希望能解決問題

+0

這讓我更加接近!在進行建議的更改之後,我現在能夠在日誌中看到一些有用的反饋,並且還向我的選項添加了'muteHttpExceptions':true;'。我希望在查看Google的API文檔後,我可以找出其餘的部分。我將追加當前日誌輸出到我原來的問題。 – user7916051

0

嘗試改變contenTypectAPPLICATION_JSON

"contentType": "ctAPPLICATION_JSON", 
相關問題