nodejs How to get gmail raw messages?

时间秒杀一切 提交于 2019-12-25 01:45:12

问题


I am trying to get the full email message data with body content using the raw option of the format as described in the gmail api reference. How ever it doesn't seem to work. Below is my code:

function listMessages(auth) {
  var gmail = google.gmail('v1');
  var nextPageToken = null;

  gmail.users.messages.list({
    auth: auth,
    userId: 'me',
    pageToken: nextPageToken,
    q: ''
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }

    var msgs = response.messages;
    if (msgs.length == 0) {
      console.log('No messages found.');
    } else {
      console.log('Messages:');
      the_format = 'raw';
      for (var i = 0; i < msgs.length; i++) {
        var msg = msgs[i];
        console.log('- %s', msg.id);
        gmail.users.messages.get({
          auth: auth,
          userId: 'me',
          id: msg.id,
          format: the_format,
        }, function(err, response) {
          if (err) {
             console.log('The API returned an error: ' + err);
             return;
          }

          console.log(response);

        });
      }
    }
  });
}

And here is an example of a returned result. I don't see the raw fields and the same result is always returned when I change the 'format' (raw or minimal).

What am I missing here ?

{ id: '16xxxxxxxxxxxxxxxxxx',
  threadId: '161xxxxxxxxxxxxxxxx',
  labelIds: [ 'UNREAD', 'Label_44', 'CATEGORY_UPDATES' ],
  snippet: 'atom posted: &quot;Voici le 9e volet de notre rubrique À quoi tu joues ? Aujourd&#39;hui la Testing Team vous embarque au Japon, avec Sakura. S&#39;est perdu dans un manoir infernal, ou a défendu un',
  historyId: '11336356',
  internalDate: '1516xxxxxxxxx',
  payload:
   { partId: '',
     mimeType: 'multipart/alternative',
     filename: '',
     headers:
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ],
     body: { size: 0 },
     parts: [ [Object], [Object] ] },
  sizeEstimate: 98391 }

回答1:


You need to use:

var msgs = response.data.messages;

instead of:

var msgs = response.messages;

That's it.



来源:https://stackoverflow.com/questions/48525620/nodejs-how-to-get-gmail-raw-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!