Example with Google API
/**
* @returns {Promise}
*/
function getOAuth2Client() {
const credentials = fs.readFileSync( "credentials.json", "utf8" );
return new Promise(( resolve ) => {
authorize(JSON.parse( credentials ), ( auth ) => {
resolve( auth );
});
});
}/**
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
* @param {string} q - Google searchbox query
* @returns {Promise}
*/
async function getUnreadEmail( auth, q ) {
const gmail = google.gmail({ version: 'v1', auth }),
userId = "me";
try {
const listRsp = await gmail.users.messages.list({
userId,
q
});
if ( !listRsp.data.messages ) {
return;
}
const [ msg ] = listRsp.data.messages,
msgRsp = await gmail.users.messages.get({
userId,
id: msg.id,
format: "raw"
}),
rawBody = Buffer.from( msgRsp.data.raw || "", "base64" )
.toString( "utf8" );
return rawBody;
} catch ( e ) {
console.error( e );
}
}Last updated