我已經通過StackOverflow搜索了(小)成功,因此我向比我更專業的編碼人員尋求幫助。在Google Apps腳本中使用Gmail API搜索郵件
我讀過這個話題SO gmailapp-add-label-to-specific-message-not-the-thread這導致我GIST,我已經採取代碼標記單個消息(而不是整個線程)。
現在我需要在Google Apps腳本中使用Gmail API的搜索功能;我在Google Developer's Site here上找到了一個Javascript代碼段。我正在嘗試使用GIST中的代碼作爲示例來調整此代碼,但沒有成功。
我需要搜索功能來獲得一個消息ID的數組傳遞給另一個功能(目前工作,))。
下面是我寫的代碼:
/**
* Retrieve Messages in user's mailbox matching query.
*
* @param {String} userId User's email address. The special value 'me'
* can be used to indicate the authenticated user.
* @param {String} query String used to filter the Messages listed.
* @param {Function} callback Function to call when the request is complete.
*/
function GAPIlistMessages(userId, query) { //callback option removed from original code
// GET https://www.googleapis.com/gmail/v1/users/userId/messages
var url = 'https://www.googleapis.com/gmail/v1/users/${userId}/messages'
.replace("${userId}","me")
var headers = {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
};
var request = {
'userId': userId,
'q': query
};
var params = {
method: "GET",
contentType: "application/json",
headers: headers,
muteHttpExceptions: true, // for debugging
payload: JSON.stringify(request)
};
var check = UrlFetchApp.getRequest(url, params); // for debugging
var response = UrlFetchApp.fetch(url, params);
var result = response.getResponseCode();
if (result == '200') { // OK
var msgIds = JSON.parse(response.getContentText()).msgIds;
Logger.log(msgIds)
}
else {
// This is only needed when muteHttpExceptions == true
var err = JSON.parse(response.getContentText());
throw new Error('Error (' + result + ") " + err.error.message);
}
}
當我啓動的功能,通過這個參數: 用戶id = '我' 查詢= 'deliveredto:[email protected]'
我收到此錯誤
[16-02-24 06:37:29:218 PST] Avvio dell'esecuzione
[16-02-24 06:37:29:236 PST] ScriptApp.getOAuthToken() [0 secondi]
[16-02-24 06:37:29:237 PST] UrlFetchApp.getRequest([https://www.googleapis.com/gmail/v1/users/me/messages, {headers={Authorization=Bearer <<<token removed>>>}, method=GET, payload={"userId":"me",...) [0 secondi]
[16-02-24 06:37:29:308 PST] UrlFetchApp.fetch([https://www.googleapis.com/gmail/v1/users/me/messages, {headers={Authorization=Bearer <<<token removed>>>}, method=GET, payload={"userId":"me",...) [0,07 secondi]
[16-02-24 06:37:29:308 PST] HTTPResponse.getResponseCode() [0 secondi]
[16-02-24 06:37:29:308 PST] HTTPResponse.getContentText() [0 secondi]
[16-02-24 06:37:29:311 PST] Esecuzione non riuscita: Error: Error (400) 'raw' RFC822 payload message string or uploading message via /upload/* URL required (riga 212, file "Test Tools") [0,075 secondi di esecuzione totale]
我覺得我親近的解決方案,但我想不出問題出在哪裏。 任何幫助將是非常讚賞
曼努埃爾
僅供參考,有一項Gmail高級服務,可以消除處理URL和身份驗證的麻煩:https://developers.google.com/apps-script/advanced/gmail –
我正在清理代碼,而我發現你是對的。現在我必須瞭解如何以正確的方式使用高級服務。謝謝 –