From 75da08f8d367809c3343d3363f243639692e3155 Mon Sep 17 00:00:00 2001 From: Lev Date: Fri, 18 Dec 2020 15:21:11 +0200 Subject: [PATCH 1/3] Added refresh_access_token function to gmail-tester.js. --- .prettierrc | 7 ++++ gmail-tester.js | 36 ++++++++++++++++---- package-lock.json | 86 +++++++++++++++++++++++------------------------ package.json | 2 +- 4 files changed, 81 insertions(+), 50 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..702f7a0 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "semi": true, + "singleQuote": false, + "printWidth": 82, + "trailingComma": "none", + "arrowParens": "avoid" +} diff --git a/gmail-tester.js b/gmail-tester.js index b46f5f5..d4acbc2 100644 --- a/gmail-tester.js +++ b/gmail-tester.js @@ -183,18 +183,42 @@ async function check_inbox( */ async function get_messages(credentials_json, token_path, options) { try { - const emails = await _get_recent_email( - credentials_json, - token_path, - options - ); + const emails = await _get_recent_email(credentials_json, token_path, options); return emails; } catch (err) { console.log("[gmail] Error:", err); } } +async function refresh_access_token(credentials_json, token_path) { + const content = JSON.parse(fs.readFileSync(credentials_json)); + const oAuth2Client = await gmail.authorize(content, token_path); + const refresh_token_result = await oAuth2Client.refreshToken( + oAuth2Client.credentials.refresh_token + ); + if (refresh_token_result && refresh_token_result.tokens) { + const new_token = JSON.parse(fs.readFileSync(token_path)); + if (refresh_token_result.tokens.access_token) { + new_token.access_token = refresh_token_result.tokens.access_token; + } + if (refresh_token_result.tokens.refresh_token) { + new_token.refresh_token = refresh_token_result.tokens.refresh_token; + } + if (refresh_token_result.tokens.expiry_date) { + new_token.expiry_date = refresh_token_result.tokens.expiry_date; + } + fs.writeFileSync(token_path, JSON.stringify(new_token)); + } else { + throw new Error( + `Refresh access token failed! Respose: ${JSON.stringify( + refresh_token_result + )}` + ); + } +} + module.exports = { check_inbox, - get_messages + get_messages, + refresh_access_token }; diff --git a/package-lock.json b/package-lock.json index dc67ec0..7bbfe00 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "gmail-tester", - "version": "1.3.0", + "version": "1.3.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -13,9 +13,9 @@ } }, "agent-base": { - "version": "6.0.1", - "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.1.tgz", - "integrity": "sha512-01q25QQDwLSsyfhrKbn8yuur+JNw0H+0Y4JiGIKd3z9aYk/w/2kxD/Upc+t2ZBBSUNff50VjPsSW2YxM8QYKVg==", + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", + "integrity": "sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==", "requires": { "debug": "4" } @@ -26,14 +26,14 @@ "integrity": "sha512-3duEwti880xqi4eAMN8AyR4a0ByT90zoYdLlevfrvU43vb0YZwZVfxOgxWrLXXXpyugL0hNZc9G6BiB5B3nUug==" }, "base64-js": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz", - "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==" + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==" }, "bignumber.js": { - "version": "9.0.0", - "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz", - "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==" + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.1.tgz", + "integrity": "sha512-IdZR9mh6ahOBv/hYGiXyVuyCetmGJhtYkqLBpTStdhEGjegpPlUawydyaF3pbIOFynJTpllEs+NP+CS9jKFLjA==" }, "buffer-equal-constant-time": { "version": "1.0.1", @@ -41,11 +41,11 @@ "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=" }, "debug": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", - "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", + "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", "requires": { - "ms": "^2.1.1" + "ms": "2.1.2" } }, "ecdsa-sig-formatter": { @@ -72,9 +72,9 @@ "integrity": "sha512-dtm4QZH9nZtcDt8qJiOH9fcQd1NAgi+K1O2DbE6GG1PPCK/BWfOH3idCTRQ4ImXRUOyopDEgDEnVEE7Y/2Wrig==" }, "gaxios": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/gaxios/-/gaxios-3.1.0.tgz", - "integrity": "sha512-DDTn3KXVJJigtz+g0J3vhcfbDbKtAroSTxauWsdnP57sM5KZ3d2c/3D9RKFJ86s43hfw6WULg6TXYw/AYiBlpA==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/gaxios/-/gaxios-4.1.0.tgz", + "integrity": "sha512-vb0to8xzGnA2qcgywAjtshOKKVDf2eQhJoiL6fHhgW5tVN7wNk7egnYIO9zotfn3lQ3De1VPdf7V5/BWfCtCmg==", "requires": { "abort-controller": "^3.0.0", "extend": "^3.0.2", @@ -84,26 +84,26 @@ } }, "gcp-metadata": { - "version": "4.1.4", - "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-4.1.4.tgz", - "integrity": "sha512-5J/GIH0yWt/56R3dNaNWPGQ/zXsZOddYECfJaqxFWgrZ9HC2Kvc5vl9upOgUUHKzURjAVf2N+f6tEJiojqXUuA==", + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-4.2.1.tgz", + "integrity": "sha512-tSk+REe5iq/N+K+SK1XjZJUrFPuDqGZVzCy2vocIHIGmPlTGsa8owXMJwGkrXr73NO0AzhPW4MF2DEHz7P2AVw==", "requires": { - "gaxios": "^3.0.0", + "gaxios": "^4.0.0", "json-bigint": "^1.0.0" } }, "google-auth-library": { - "version": "6.0.6", - "resolved": "https://registry.npmjs.org/google-auth-library/-/google-auth-library-6.0.6.tgz", - "integrity": "sha512-fWYdRdg55HSJoRq9k568jJA1lrhg9i2xgfhVIMJbskUmbDpJGHsbv9l41DGhCDXM21F9Kn4kUwdysgxSYBYJUw==", + "version": "6.1.3", + "resolved": "https://registry.npmjs.org/google-auth-library/-/google-auth-library-6.1.3.tgz", + "integrity": "sha512-m9mwvY3GWbr7ZYEbl61isWmk+fvTmOt0YNUfPOUY2VH8K5pZlAIWJjxEi0PqR3OjMretyiQLI6GURMrPSwHQ2g==", "requires": { "arrify": "^2.0.0", "base64-js": "^1.3.0", "ecdsa-sig-formatter": "^1.0.11", "fast-text-encoding": "^1.0.0", - "gaxios": "^3.0.0", - "gcp-metadata": "^4.1.0", - "gtoken": "^5.0.0", + "gaxios": "^4.0.0", + "gcp-metadata": "^4.2.0", + "gtoken": "^5.0.4", "jws": "^4.0.0", "lru-cache": "^6.0.0" } @@ -117,21 +117,21 @@ } }, "googleapis": { - "version": "59.0.0", - "resolved": "https://registry.npmjs.org/googleapis/-/googleapis-59.0.0.tgz", - "integrity": "sha512-GV/E4KRN89a4GxSk7D7cwUfRYgcJHR05sOgm/WGdwc/u8dxNXG5lWmz9gF5ZwFGk2yKtVxL4VZNn4zBuZ6rmGg==", + "version": "66.0.0", + "resolved": "https://registry.npmjs.org/googleapis/-/googleapis-66.0.0.tgz", + "integrity": "sha512-jdEleRoyo/AeJZjKGC7Z2mHgochn2vR2JKqey6kydRkIBmCZxoQKLisRR4H8CRYZeEd6+c8Ns/LzS1S7qUjoFw==", "requires": { "google-auth-library": "^6.0.0", - "googleapis-common": "^4.4.0" + "googleapis-common": "^4.4.1" } }, "googleapis-common": { - "version": "4.4.0", - "resolved": "https://registry.npmjs.org/googleapis-common/-/googleapis-common-4.4.0.tgz", - "integrity": "sha512-Bgrs8/1OZQFFIfVuX38L9t48rPAkVUXttZy6NzhhXxFOEMSHgfFIjxou7RIXOkBHxmx2pVwct9WjKkbnqMYImQ==", + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/googleapis-common/-/googleapis-common-4.4.3.tgz", + "integrity": "sha512-W46WKCk3QtlCCfmZyQIH5zxmDOyeV5Qj+qs7nr2ox08eRkEJMWp6iwv542R/PsokXaGUSrmif4vCC4+rGzRSsQ==", "requires": { "extend": "^3.0.2", - "gaxios": "^3.0.0", + "gaxios": "^4.0.0", "google-auth-library": "^6.0.0", "qs": "^6.7.0", "url-template": "^2.0.8", @@ -139,12 +139,12 @@ } }, "gtoken": { - "version": "5.0.3", - "resolved": "https://registry.npmjs.org/gtoken/-/gtoken-5.0.3.tgz", - "integrity": "sha512-Nyd1wZCMRc2dj/mAD0LlfQLcAO06uKdpKJXvK85SGrF5+5+Bpfil9u/2aw35ltvEHjvl0h5FMKN5knEU+9JrOg==", + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/gtoken/-/gtoken-5.1.0.tgz", + "integrity": "sha512-4d8N6Lk8TEAHl9vVoRVMh9BNOKWVgl2DdNtr3428O75r3QFrF/a5MMu851VmK0AA8+iSvbwRv69k5XnMLURGhg==", "requires": { - "gaxios": "^3.0.0", - "google-p12-pem": "^3.0.0", + "gaxios": "^4.0.0", + "google-p12-pem": "^3.0.3", "jws": "^4.0.0", "mime": "^2.2.0" } @@ -234,9 +234,9 @@ "integrity": "sha1-/FZaPMy/93MMd19WQflVV5FDnyE=" }, "uuid": { - "version": "8.3.0", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.0.tgz", - "integrity": "sha512-fX6Z5o4m6XsXBdli9g7DtWgAx+osMsRRZFKma1mIUsLCz6vRvv+pz5VNbyu9UEDzpMWulZfvpgb/cmDXVulYFQ==" + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" }, "yallist": { "version": "4.0.0", diff --git a/package.json b/package.json index eb5d10e..d47a06b 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "author": "Lev Gelfenbuim", "license": "MIT", "dependencies": { - "googleapis": "^59.0.0" + "googleapis": "^66.0.0" }, "homepage": "https://github.com/levz0r/gmail-tester" } From 33f1ee03bfd9e448e50c2162a85e3bcda282a450 Mon Sep 17 00:00:00 2001 From: Lev Date: Mon, 18 Jan 2021 23:41:55 +0200 Subject: [PATCH 2/3] Added refresh_access_token to README.md. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 4f0eeee..3053388 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,13 @@ An array of `email` objects with the following fields:
In addition, verbose messages will be written to console. +### `refresh_access_token(credentials_json, token_path)` + +`credentials_json`: Path to credentials JSON file.
+`token_path`: Path to existing OAuth2 token file.
+ +Refresh the access token. A new file will overwrite the existing one in `token_path`. + # Example ## Using `check_inbox()` to look for a specific message: From c816c461ffa653d8e29088d9f00ee179aea4f1aa Mon Sep 17 00:00:00 2001 From: Lev Date: Mon, 18 Jan 2021 23:44:59 +0200 Subject: [PATCH 3/3] package.json: Bump version + Close #50. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d47a06b..e2e4f1c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gmail-tester", - "version": "1.3.1", + "version": "1.3.2", "description": "A simple NodeJS gmail client which checks the inbox for specific message existance", "main": "gmail-tester.js", "scripts": {},