gpt4 book ai didi

javascript - 你如何使用 nodejs 下载 Gmail API 的附件?

转载 作者:行者123 更新时间:2023-11-30 06:24:39 25 4
gpt4 key购买 nike

我正在尝试在我的本地计算机上制作一个执行此操作的 Node 应用程序:

1) 查看我的 Gmail 收件箱。

2) 如果电子邮件有特定标签和附件,请下载。

我在第 2 步。

我感到困惑的部分是关于“零件”的部分。

https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get

谷歌示例代码:

function getAttachments(userId, message, callback) {
var parts = message.payload.parts;
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (part.filename && part.filename.length > 0) {
var attachId = part.body.attachmentId;
var request = gapi.client.gmail.users.messages.attachments.get({
'id': attachId,
'messageId': message.id,
'userId': userId
});
request.execute(function(attachment) { // <--- that attachment param
callback(part.filename, part.mimeType, attachment);
});
}
}
}

“附件”似乎包含附件数据。

这是我的代码:

const gmail = google.gmail({version: 'v1', auth});

gmail.users.messages.list({
userId: 'me',
'q':'subject:my-test has:nouserlabels has:attachment'
}, (err, res) => {
if (err) return console.log('1 The API returned an error: ' + err);

const msgs = res.data.messages;

if(typeof msgs !== 'undefined'){
console.log('Messages:');
msgs.forEach( msg => {

console.log(`- ${msg.id}`);

gmail.users.messages.get({
userId: 'me',
id: msg.id,
format:'full'
},(err, res) => {
if (err) return console.log('2 The API returned an error: ' + err);

// Wheres the data????? //

console.log('A')
console.log(res.data.payload.parts)

console.log('B')
console.log(res.data.payload.parts[1])

console.log('C')
console.log(res.data.payload.parts[1].body)
})
});
} else {
console.log('no messages found')
}


});

最佳答案

我刚刚用它从我最喜欢的会计师那里检索文档。

const fsPromises = require('fs/promises');
const path = require('path');
const {google} = require('googleapis');
const readline = require('readline');
const fs = require('fs');

.... code from https://developers.google.com/gmail/api/quickstart/nodejs for auth

const fromEmail = process.argv[2];
const shortFromEmail = fromEmail.substring(0, fromEmail.indexOf('@'));
/**
* Lists the labels in the user's account.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
async function main(auth) {
const gmail = google.gmail({version: 'v1', auth})
// console.log('list messages with attachments auth: ' + JSON.stringify(auth))
const {data} = await gmail.users.messages.list({
userId: 'me',
'q':`from:${fromEmail} has:nouserlabels has:attachment`,
// maxResults: 5
});
console.log(data);
let {messages} = data;
await messages.reduce(async function (accProm, messageId) {
await accProm;
const {data: message} = await gmail.users.messages.get({
userId: 'me',
id: messageId.id,
format: 'full'
});
// console.log(JSON.stringify(message, null, 2));
let messagePayloadParts = message.payload.parts
const attachments = await messagePayloadParts.reduce(async function (acc2Prom, part) {
const acc2 = await acc2Prom;
if (!part.body.size || part.body.attachmentId == undefined) {
return acc2;
}
const internalDate = new Date(parseInt(message.internalDate, 10));
const datestring = internalDate.getFullYear() + " " + (internalDate.getMonth()+1).toString().padStart(2, "0") + " " + internalDate.getDate().toString().padStart(2, "0");
console.log(datestring, shortFromEmail, part.filename, part)
let fileExt;
switch (part.mimeType) {
case 'application/octet-stream': fileExt = ''; break;
case 'application/pdf': fileExt = 'pdf'; break;
case 'message/rfc822': fileExt = 'eml'; break;
case 'image/jpeg': fileExt = 'jpg'; break;
case 'image/png': fileExt = 'png'; break;
case 'application/msword': fileExt = 'doc'; break;
default: console.error('unknownMimeType', part.mimeType); boom;
}
const dirname = `${datestring} ${shortFromEmail}`;
const filename = part.filename ? part.filename : `noname_${part.body.attachmentId.substring(0,8)}.${fileExt}`
const attachmentPath = path.join(dirname, filename);
const s = await fsPromises.stat(attachmentPath).catch(e => undefined);
if (s) { return acc2 };
const {data: attachment} = await gmail.users.messages.attachments.get({
userId: 'me',
messageId: message.id,
id: part.body.attachmentId,
});
const { size, data: dataB64 } = attachment;
await fsPromises.mkdir(dirname, {recursive: true});
await fsPromises.writeFile(attachmentPath, Buffer.from(dataB64, 'base64'));
return acc2;
// console.log('callback: ' + JSON.stringify(attachments))
}, Promise.resolve([]));
}, Promise.resolve());
}

关于javascript - 你如何使用 nodejs 下载 Gmail API 的附件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51216410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com