//getBodyContent.js
/**
* Get the recent email from your Gmail account
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
'use strict';
const fs = require('fs');
const {promisify} = require('util');
const {google} = require('googleapis');
const {OAuth2Client} = require('google-auth-library');
const gmail = google.gmail('v1');
// Promisify with promise
const readFileAsync = promisify(fs.readFile);
const gmailGetMessagesAsync = promisify(gmail.users.messages.get);
const gmailListMessagesAsync = promisify(gmail.users.messages.list);
const TOKEN_DIR = __dirname;
const TOKEN_PATH = TOKEN_DIR + '/gmail-nodejs-quickstart.json'; // Specify the access token file
const main = async () => {
// Get credential information & specify the client secret file
const content = await readFileAsync(__dirname+'/client_secret.json');
const credentials = JSON.parse(content); // credential
// authentication
const clientSecret = credentials.installed.client_secret;
const clientId = credentials.installed.client_id;
const redirectUrl = credentials.installed.redirect_uris[0];
const oauth2Client = new OAuth2Client(clientId, clientSecret, redirectUrl);
const token = await readFileAsync(TOKEN_PATH);
oauth2Client.credentials = JSON.parse(token);
// Access the gmail via API
let res = await gmailListMessagesAsync({
auth: oauth2Client,
userId: "me",
maxResults: 1,
// Only get the recent email - 'maxResults' parameter
});
// Get the message id which we will need to retreive tha actual message next.
const newestMessageId = res["data"]["messages"][0]["id"];
// Retreive the actual message using the message id
res = await gmailGetMessagesAsync({
auth: oauth2Client,
userId: "me",
id: newestMessageId,
});
//Then we will need to decode the base64 encoded message.
let body_content = JSON.stringify(res.data.payload.body.data);
let data, buff, text;
data = body_content;
buff = new Buffer.from(data, "base64");
mailBody = buff.toString();
// display the result
console.log(mailBody);
};
main();
This is my code so far. I got it from this website https://medium.com/tech-learn-share/lets-receive-gmail-from-node-js-using-google-official-library-6a6280254325 It's supposed to get the body of the most recent email in my gmail. When I run it i get this error:
(node:2247) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'context' of undefined
at list (/Users/fazl/Coding/Js/node_modules/googleapis/build/src/apis/gmail/v1.js:785:31)
at internal/util.js:297:30
at new Promise (<anonymous>)
at list (internal/util.js:296:12)
at main (/Users/fazl/Coding/Js/index.js:38:17)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2247) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:2247) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
What does this mean? What did I do wrong and how do i fix it?
question from:
https://stackoverflow.com/questions/65650801/how-can-i-get-the-body-of-an-email-node-js-javascript-gmail-api 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…