问题
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: "Voici le 9e volet de notre rubrique À quoi tu joues ? Aujourd'hui la Testing Team vous embarque au Japon, avec Sakura. S'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