【发布时间】:2014-10-10 18:37:38
【问题描述】:
在下面的代码中,我正在登录、授权应用程序并通过 GMail API 获取控制台输出。我相信我得到了线程和线程 ID,但我没有在控制台中看到消息。
我没有收到任何错误,我收到了输出,就像没有值的键。
控制台输出如下所示:
代码如下:
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”。 当我记录 dataItem.value 时,我得到一个 dataMessage.messages 未定义,并且不能有 0 的索引。
非常感谢帮助您完成这项工作!
【问题讨论】:
标签: javascript jquery gmail-api