1

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.

BJ Myers
  • 6,617
  • 6
  • 34
  • 50

2 Answers2

3

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:

Community
  • 1
  • 1
iCrus
  • 1,210
  • 16
  • 30
1
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.

Januka samaranyake
  • 2,385
  • 1
  • 28
  • 50