gh api auth проблема - не может получить вложение от выбранного электронного письма - PullRequest
2 голосов
/ 07 апреля 2020

Я работаю с node.js v13 с Gmail API. Я успешно получил указанное c сообщение электронной почты, и теперь я пытаюсь получить вложение из этого же электронного письма.

function getRecentEmail(auth) {
// Only get the recent email - 'maxResults' parameter
gmail.users.messages.list({ auth: auth, userId: 'me', maxResults: 1, q: 'subject:pokerrrr' }, function (
    err,
    response
) {
    if (err) {
        console.log('The API returned an error: ' + err);
        return;
    }

    // Get the message id which we will need to retreive tha actual message next.
    const message_id = response['data']['messages'][0]['id'];

    // Retreive the actual message using the message id
    gmail.users.messages.get({ auth: auth, userId: 'me', id: message_id }, function (err, response) {
        if (err) {
            console.log('The API returned an error: ' + err);
            return;
        }
        // const attachment_id = response['data'].payload.parts[1].body.attachmentId;
        const message = response['data'];
        // getAttachments('me', message, auth);
        const parts = message.payload.parts;
        for (let i = 0; i < parts.length; i++) {
            const part = parts[i];
            if (part.filename && part.filename.length > 0) {
                const attachId = part.body.attachmentId;
                gmail.users.messages.attachments.get(
                    {
                        id: attachId,
                        messageId: message.id,
                        userId: 'me',
                    },
                    function (attachment) {

                        // saveAttachmentsToDir(part.filename, part.mimeType, attachment);
                    }
                );
            }
        }
    });
});

}

, когда я запускаю приведенный выше код, параметр вложения показывает мне ошибку аутентификации, и я не могу понять, почему. здесь ошибка:

code:401
config:Object {url: "https://www.googleapis.com/gmail/v1/users/me/messa…", method: "GET", paramsSerializer: , …}
errors:Array(1) [Object]
message:"Login Required"
response:Object {config: Object, data: Object, headers: Object, …}
stack:"Error: Login Required
    at Gaxios.<anonymous> (c:\Users\tomer\Desktop\Mail listener -tomer\Mail listener\node_modules\gaxios\build\src\gaxios.js:73:27)
    at Generator.next (<anonymous>)
    at fulfilled (c:\Users\tomer\Desktop\Mail listener -tomer\Mail listener\node_modules\gaxios\build\src\gaxios.js:16:58)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)"
__proto__:Error {constructor: }
...