В приведенном ниже коде я вхожу в систему, авторизую приложение и получаю вывод консоли через GMail API. Я считаю, что получаю потоки и идентификаторы потоков, но не вижу сообщений в консоли.
Я не получаю никаких ошибок, и я получаю вывод, который выглядит как ключи без значений.
Вот как выглядит вывод консоли:
Вот код:
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);
}
Как получить адрес отправителя, тему и текст сообщения? с API GMAIL в js
** Обновление: с чем я сейчас работаю: **
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, «Требуется идентификатор». Когда я регистрирую dataItem.value, я получаю, что dataMessage.messages не определено и не может иметь индекс 0.
Я был бы очень признателен за помощь в получении этой работы!