Skip to content

Commit

Permalink
Merge branch 'feature/refresh_token'
Browse files Browse the repository at this point in the history
  • Loading branch information
levz0r committed Jan 18, 2021
2 parents ebc233b + c816c46 commit 24a9bbc
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 51 deletions.
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"singleQuote": false,
"printWidth": 82,
"trailingComma": "none",
"arrowParens": "avoid"
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ An array of `email` objects with the following fields:<br>

In addition, verbose messages will be written to console.

### `refresh_access_token(credentials_json, token_path)`

`credentials_json`: Path to credentials JSON file.<br>
`token_path`: Path to existing OAuth2 token file.<br>

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:
Expand Down
36 changes: 30 additions & 6 deletions gmail-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
86 changes: 43 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {},
Expand All @@ -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"
}

0 comments on commit 24a9bbc

Please sign in to comment.