From af363bc064438256c0b8eab420d6338c0bb6918b Mon Sep 17 00:00:00 2001 From: Revadike <4411977+Revadike@users.noreply.github.com> Date: Wed, 26 Jul 2023 14:08:22 +0200 Subject: [PATCH 1/5] Added port --- gmail.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gmail.js b/gmail.js index 4d9a128..9c9bf95 100644 --- a/gmail.js +++ b/gmail.js @@ -14,7 +14,7 @@ const SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]; * @param {string | Object} token Token. * @return {google.auth.OAuth2} The OAuth2Client. */ -async function authorize(credentials, token) { +async function authorize(credentials, token, port = 32019) { const { client_secret, client_id, redirect_uris } = _get_credentials_object(credentials).installed; const oAuth2Client = new google.auth.OAuth2( @@ -27,7 +27,7 @@ async function authorize(credentials, token) { oAuth2Client.setCredentials(_get_token_object(token)); return oAuth2Client; } catch (error) { - const newOAuth2Client = await get_new_token(oAuth2Client, token); + const newOAuth2Client = await get_new_token(oAuth2Client, token, port = 32019); if (token instanceof Object) { tokenStore.store(newOAuth2Client.credentials); } else { @@ -43,8 +43,8 @@ async function authorize(credentials, token) { * @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for. * @return {Promise} The promise for the authorized client. */ -async function get_new_token(oAuth2Client, token) { - return authenticate(oAuth2Client, SCOPES, token); +async function get_new_token(oAuth2Client, token, port = 32019) { + return authenticate(oAuth2Client, SCOPES, token, port); } /** From d4769ce66c1da4030c66ce27c10635d6b07cc8e4 Mon Sep 17 00:00:00 2001 From: Revadike <4411977+Revadike@users.noreply.github.com> Date: Wed, 26 Jul 2023 14:18:09 +0200 Subject: [PATCH 2/5] Added port --- gmail-tester.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/gmail-tester.js b/gmail-tester.js index 4e791a3..5ff3a42 100644 --- a/gmail-tester.js +++ b/gmail-tester.js @@ -30,16 +30,17 @@ function _init_query(options) { return query; } -async function _get_recent_email(credentials, token, options = {}) { +async function _get_recent_email(credentials, token, options = {}, port = 32019) { const emails = []; const query = _init_query(options); // Load client secrets from a local file. - const oAuth2Client = await gmail.authorize(credentials, token); + const oAuth2Client = await gmail.authorize(credentials, token, port); const gmail_emails = await gmail.get_recent_email( oAuth2Client, query, - options.label + options.label, + port ); for (const gmail_email of gmail_emails) { const email = { @@ -96,7 +97,7 @@ async function _get_recent_email(credentials, token, options = {}) { return emails; } -async function __check_inbox(credentials, token, options = {}) { +async function __check_inbox(credentials, token, options = {}, port = 32019) { const { subject, from, to, wait_time_sec, max_wait_time_sec } = options; try { console.log( @@ -108,7 +109,8 @@ async function __check_inbox(credentials, token, options = {}) { const emails = await _get_recent_email( credentials, token, - options + options, + port ); if (emails.length > 0) { console.log(`[gmail] Found!`); @@ -147,6 +149,7 @@ async function __check_inbox(credentials, token, options = {}) { * @param {number} [options.wait_time_sec] - Interval between inbox checks (in seconds). Default: 30 seconds. * @param {number} [options.max_wait_time_sec] - Maximum wait time (in seconds). When reached and the email was not found, the script exits. Default: 60 seconds. * @param {string} [options.label] - String. The default label is 'INBOX', but can be changed to 'SPAM', 'TRASH' or a custom label. For a full list of built-in labels, see https://developers.google.com/gmail/api/guides/labels?hl=en + * @param {number} [port] - Optional port option, in case the default port (32019) is unavailable. */ async function check_inbox( credentials, @@ -159,7 +162,8 @@ async function check_inbox( max_wait_time_sec: 30, include_body: false, label: "INBOX" - } + }, + port = 32019 ) { if (typeof options !== "object") { console.error( @@ -167,7 +171,7 @@ async function check_inbox( ); process.exit(1); } - return __check_inbox(credentials, token, options); + return __check_inbox(credentials, token, options, port); } /** @@ -182,10 +186,11 @@ async function check_inbox( * @param {string} options.subject - Filter on the subject of the email. * @param {Object} options.before - Date. Filter messages received _after_ the specified date. * @param {Object} options.after - Date. Filter messages received _before_ the specified date. + * @param {number} [port] - Optional port option, in case the default port (32019) is unavailable. */ -async function get_messages(credentials, token, options) { +async function get_messages(credentials, token, options, port = 32019) { try { - return await _get_recent_email(credentials, token, options); + return await _get_recent_email(credentials, token, options, port = 32019); } catch (err) { console.log("[gmail] Error:", err); } @@ -196,9 +201,10 @@ async function get_messages(credentials, token, options) { * * @param {string | Object} credentials - Path to credentials json file or credentials Object. * @param {string | Object} token - Path to token json file or token Object. + * @param {number} [port] - Optional port option, in case the default port (32019) is unavailable. */ -async function refresh_access_token(credentials, token) { - const oAuth2Client = await gmail.authorize(credentials, token); +async function refresh_access_token(credentials, token, port = 32019) { + const oAuth2Client = await gmail.authorize(credentials, token, port = 32019); const refresh_token_result = await oAuth2Client.refreshToken( oAuth2Client.credentials.refresh_token ); From 22250e9119f6ecdfd81a08d53327130e422cb6db Mon Sep 17 00:00:00 2001 From: Revadike <4411977+Revadike@users.noreply.github.com> Date: Wed, 26 Jul 2023 14:23:38 +0200 Subject: [PATCH 3/5] JSDoc --- gmail.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gmail.js b/gmail.js index 9c9bf95..b8c8006 100644 --- a/gmail.js +++ b/gmail.js @@ -12,6 +12,7 @@ const SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]; * given callback function. * @param {string | Object} credentials The authorization client credentials. * @param {string | Object} token Token. + * @param {number} [port] - Optional port option, in case the default port (32019) is unavailable. * @return {google.auth.OAuth2} The OAuth2Client. */ async function authorize(credentials, token, port = 32019) { @@ -41,6 +42,8 @@ async function authorize(credentials, token, port = 32019) { * Get a new token after prompting for user authorization, and then * execute the given callback with the authorized OAuth2 client. * @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for. + * @param {string | Object} token Token. + * @param {number} [port] - Optional port option, in case the default port (32019) is unavailable. * @return {Promise} The promise for the authorized client. */ async function get_new_token(oAuth2Client, token, port = 32019) { From d78b3f4eb5ade6215f6862cfc245e3ea6e8bb5c9 Mon Sep 17 00:00:00 2001 From: Revadike <4411977+Revadike@users.noreply.github.com> Date: Wed, 26 Jul 2023 14:27:21 +0200 Subject: [PATCH 4/5] Added port --- libs/oauth2.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/libs/oauth2.js b/libs/oauth2.js index 02117ea..b4c761b 100644 --- a/libs/oauth2.js +++ b/libs/oauth2.js @@ -32,23 +32,27 @@ if (fs.existsSync(keyPath)) { /** * Open an http server to accept the oauth callback. In this simple example, the only request to our webserver is to /callback?code= */ -async function authenticate(oauth2Client, scopes, tokensFile) { +async function authenticate(oauth2Client, scopes, tokensFile, port = 80) { return new Promise((resolve, reject) => { // grab the url that will be used for authorization if (!fs.existsSync(tokensFile)) { const authorizeUrl = oauth2Client.generateAuthUrl({ access_type: "offline", - scope: scopes.join(" ") + scope: scopes.join(" "), + redirect_uri: `http://localhost:${port}` }); console.log(`Authorize this app by visiting this url: ${authorizeUrl}`); const server = http .createServer(async (req, res) => { try { if (req.url.indexOf("/") > -1) { - const qs = new url.URL(req.url, "http://localhost:80").searchParams; + const qs = new url.URL(req.url, `http://localhost:${port}`).searchParams; res.end("Authentication successful! Please return to the console."); server.destroy(); - const { tokens } = await oauth2Client.getToken(qs.get("code")); + const { tokens } = await oauth2Client.getToken({ + code: qs.get("code"), + redirect_uri: `http://localhost:${port}` + }); fs.writeFileSync(tokensFile, JSON.stringify(tokens, null, 2)); oauth2Client.credentials = tokens; // eslint-disable-line require-atomic-updates resolve(oauth2Client); @@ -57,7 +61,7 @@ async function authenticate(oauth2Client, scopes, tokensFile) { reject(e); } }) - .listen(80, () => { + .listen(port, () => { // open the browser to the authorize url to start the workflow opn(authorizeUrl, { wait: false }).then(cp => cp.unref()); }); From 36b57bb32136a561dacee3855f1a7e5a767013d5 Mon Sep 17 00:00:00 2001 From: Revadike <4411977+Revadike@users.noreply.github.com> Date: Wed, 26 Jul 2023 14:32:30 +0200 Subject: [PATCH 5/5] Update gmail-tester.js --- gmail-tester.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gmail-tester.js b/gmail-tester.js index 5ff3a42..43788a7 100644 --- a/gmail-tester.js +++ b/gmail-tester.js @@ -39,8 +39,7 @@ async function _get_recent_email(credentials, token, options = {}, port = 32019) const gmail_emails = await gmail.get_recent_email( oAuth2Client, query, - options.label, - port + options.label ); for (const gmail_email of gmail_emails) { const email = {