在下面的代码中,我将登录,授权应用程序,并通过GMail API获得控制台输出。我相信我正在获得线程和线程in,但我没有看到控制台中的消息。
我没有得到任何错误,我得到了输出,就像没有值的键。
以下是控制台输出的样子:
以下是代码:
var CLIENT_ID = 'YOUR_CLIENT_ID';
var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
var USER = 'me';
/**
* Called when the client library is loaded to start the auth flow.
*/
function handleClientLoad() {
window.setTimeout(checkAuth, 1);
}
/**
* Check if the current user has authorized the application.
*/
function checkAuth() {
gapi.auth.authorize(
{'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': true},
handleAuthResult);
}
/**
* Called when authorization server replies.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
var authButton = document.getElementById('authorizeButton');
var outputNotice = document.getElementById('notice');
authButton.style.display = 'none';
outputNotice.style.display = 'block';
if (authResult && !authResult.error) {
// Access token has been successfully retrieved, requests can be sent to the API.
gapi.client.load('gmail', 'v1', function() {
listThreads(USER, function(resp) {
var threads = resp.threads;
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
console.log(thread);
console.log(thread['id']);
}
});
});
} else {
// No access token could be retrieved, show the button to start the authorization flow.
authButton.style.display = 'block';
outputNotice.style.display = 'none';
authButton.onclick = function() {
gapi.auth.authorize(
{'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': false},
handleAuthResult);
};
}
}
/**
* Get a page of Threads.
*
* @param {String} userId User's email address. The special value 'me'
* can be used to indicate the authenticated user.
* @param {Function} callback Function called when request is complete.
*/
function listThreads(userId, callback) {
var request = gapi.client.gmail.users.threads.list({
'userId': userId
});
request.execute(callback);
}
如何检索消息的发件人地址、主题和正文?使用js中的GMAIL API
**最新情况:我目前正在从事的工作:**
listThreads('me', function(dataResult){
$.each(dataResult, function(i, item){
getThread('me', item.id, function(dataMessage){
console.log(dataMessage);
var temp = dataMessage.messages[0].payload.headers;
$.each(temp, function(j, dataItem){
if(dataItem.name == 'From'){
console.log(dataItem.value);
}
});
});
});
});
当我记录dataMessage时,我会得到一个400个错误,“id required”。当我记录dataItem.value时,我得到一个dataMessage.messages是未定义的,并且不能有0的索引。
我将非常感谢帮助使这一工作!
发布于 2014-08-18 16:24:24
发布于 2014-09-05 07:33:08
这里是我从电子邮件id 从message获得的过程
调用listThread()
方法之后,我调用getThread()
方法从该线程获取电子邮件ID,如下所示。
listThreads("me", "", function (dataResult) {
$.each(dataResult, function (i, item) {
getThread("me", item.id, function (dataMessage) {
var temp = dataMessage.messages[0].payload.headers;
$.each(temp, function (j, dataItem) {
if (dataItem.name == "From") {
Console.log(dataItem.value);
}
});
});
});
});
类似地,您可以从消息中获取其他详细信息。
参考:消息的JSON格式
发布于 2014-09-05 16:06:20
正如上面Amit所说,您可以使用messages.list()来获取消息ids的列表。通过这些方法,您可以简单地调用messages.get(),这将以解析的形式返回一封电子邮件,您可以通过message.payload.headers到达标头。您不需要获得“原始”消息base64编码。
https://stackoverflow.com/questions/25353304
复制相似问题