Google apps script 在谷歌应用程序脚本中使用Gmail API搜索邮件
我在StackOverflow中搜索到了(很小的)成功,所以我向一个比我更专业的程序员寻求帮助 我已经阅读了这个主题,这就引出了这个主题,在这里我使用了标记单个消息(而不是整个线程)的代码 现在我需要在谷歌应用程序脚本中使用Gmail API的搜索功能;我在谷歌开发者的网站上发现了一个Javascript代码片段。我试图以GIST中的代码为例来修改这段代码,但没有成功 我需要搜索函数来获取一个消息ID数组,以传递给另一个函数(目前可以使用;) 以下是我编写的代码:Google apps script 在谷歌应用程序脚本中使用Gmail API搜索邮件,google-apps-script,gmail-api,Google Apps Script,Gmail Api,我在StackOverflow中搜索到了(很小的)成功,所以我向一个比我更专业的程序员寻求帮助 我已经阅读了这个主题,这就引出了这个主题,在这里我使用了标记单个消息(而不是整个线程)的代码 现在我需要在谷歌应用程序脚本中使用Gmail API的搜索功能;我在谷歌开发者的网站上发现了一个Javascript代码片段。我试图以GIST中的代码为例来修改这段代码,但没有成功 我需要搜索函数来获取一个消息ID数组,以传递给另一个函数(目前可以使用;) 以下是我编写的代码: /** * Retrieve
/**
* 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 );
}
}
启动函数时,传递以下参数:
userId='me'
query='deliveredto:myemail+test@mydomain.com"
我收到这个错误
[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]
[16-02-24 06:37:29:218太平洋标准时间]Avvio dell'esecuzione
[16-02-24 06:37:29:236 PST]ScriptApp.getOAuthToken()[0秒]
[16-02-24 06:37:29:237 PST]UrlFetchApp.getRequest([https://www.googleapis.com/gmail/v1/users/me/messages,{headers={Authorization=Bearer},method=GET,payload={“userId”:“me”,…)[0秒]
[16-02-24 06:37:29:308 PST]UrlFetchApp.fetch([https://www.googleapis.com/gmail/v1/users/me/messages,{headers={Authorization=Bearer},method=GET,payload={“userId”:“me”,…)[0,07 secondi]
[16-02-24 06:37:29:308 PST]HTTPResponse.getResponseCode()[0秒]
[16-02-24 06:37:29:308 PST]HTTPResponse.getContentText()[0秒]
[16-02-24 06:37:29:311 PST]Esecuzione非riuscita:错误:错误(400)“原始”RFC822有效负载消息字符串或通过/upload/*URL上传消息需要(riga 212,文件“测试工具”)[0075秒二Esecuzione Total]
我觉得我已经接近解决方案了,但我不知道问题出在哪里。
非常感谢您的帮助
Manuel如果您查看,它会说有效负载
是请求的主体。您的查询不应该在主体中,而是一个查询字符串参数:
function GAPIlistMessages(query) {
var url = 'https://www.googleapis.com/gmail/v1/users/me/messages?q=' +
encodeURIComponent(query)
var response = UrlFetchApp.getRequest(url, {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}
});
}
如果您查看,它会说有效负载
是请求的主体。您的查询不应该在主体中,而是一个查询字符串参数:
function GAPIlistMessages(query) {
var url = 'https://www.googleapis.com/gmail/v1/users/me/messages?q=' +
encodeURIComponent(query)
var response = UrlFetchApp.getRequest(url, {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}
});
}
感谢您的评论,我找到了解决方案并重写了代码:
function GAPIlistMessages(userId, query) {
var url = 'https://www.googleapis.com/gmail/v1/users/'+userId+'/messages'+'?q='+query+'&fields=messages%2Fid' // JSON array of message ID
var headers = {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
};
var params = {
method: "GET",
contentType: "application/json",
headers: headers,
muteHttpExceptions: true, // for debugging
};
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());
//Logger.log(response.getContentText())
return msgIds;
}
else {
// This is only needed when muteHttpExceptions == true
var err = JSON.parse(response.getContentText());
throw new Error( 'Error (' + result + ") " + err.error.message );
}
}
现在,我可以检索给定查询的所有消息ID,并将该列表传递给另一个用于提取附件和其他任务的函数。
这种方法的真正优点是,我可以处理单个消息,而不是使用简单的GAS命令处理整个线程。感谢您的评论,我找到了解决方案并重写了代码:
function GAPIlistMessages(userId, query) {
var url = 'https://www.googleapis.com/gmail/v1/users/'+userId+'/messages'+'?q='+query+'&fields=messages%2Fid' // JSON array of message ID
var headers = {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
};
var params = {
method: "GET",
contentType: "application/json",
headers: headers,
muteHttpExceptions: true, // for debugging
};
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());
//Logger.log(response.getContentText())
return msgIds;
}
else {
// This is only needed when muteHttpExceptions == true
var err = JSON.parse(response.getContentText());
throw new Error( 'Error (' + result + ") " + err.error.message );
}
}
现在,我可以检索给定查询的所有消息ID,并将该列表传递给另一个用于提取附件和其他任务的函数。
这种方法的真正优点是,我可以处理单个消息,而不是使用简单的GAS命令处理整个线程。FYI,有一个Gmail高级服务,它消除了处理URL和身份验证的麻烦:我正在清理代码,我发现你是对的。现在我必须了解如何使用高级服务谢谢,有一个Gmail高级服务,它消除了处理URL和身份验证的麻烦。我正在清理代码,我发现你是对的。现在我必须了解如何以正确的方式使用高级服务。谢谢