问题
I'm new in node, and I want to get info about my gmail messages, like who's wrote the message and the text. I follow quickstart exemple and write the following code, but there are just 'id' and 'threadId' field in output.
function listMessages(auth) {
var gmail = google.gmail('v1');
gmail.users.messages.list({
auth: auth,
userId: 'me'
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
var messages = response.messages;
if (messages.length == 0) {
console.log('No messages found.');
} else {
console.log('Messages:');
for (var i = 0; i < messages.length; i++) {
var message = messages[i];
console.log(message);
}
}
});
}
Could somebody explain me? Thanks.
回答1:
messages.list()
API returns only the message IDs and thread IDs by design. If you need to get details of those messages you need to make another API call messages.get()
to get specific details of each message.
API reference: https://developers.google.com/gmail/api/v1/reference/users/messages/get
While this will work, you may want to have a look at GMail's batch operations which allow getting details of messages in one call (with limitations though).
Google's guide on batch: https://developers.google.com/gmail/api/guides/batch
Similar queries have been answered here at SO:
- Gmail API all messages
- Gmail API Users.messages: list
回答2:
function getMessage(userId, messageId, callback) {
var request = gapi.client.gmail.users.messages.get({
'userId': userId,
'id': messageId
});
request.execute(callback);
}
add "me
" as userId
and add your message id in messageId
filed. others fields are optional.
来源:https://stackoverflow.com/questions/40890997/how-can-i-get-payload-property-from-the-messages-list-gmail-api