forked from pinterest/api-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh_access_token.js
executable file
·42 lines (34 loc) · 1.61 KB
/
refresh_access_token.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env node
import { ArgumentParser } from 'argparse';
import { ApiConfig } from '../src/api_config.js';
import { common_arguments } from '../src/arguments.js';
/**
* This script refreshes an access token originally created by using the
* the get_access_token.py script with the --write argument. The new access
* token may be printed with the --cleartext option.
*/
async function main(argv) {
const parser = new ArgumentParser({ description: 'Refresh Pinterest OAuth token' });
parser.add_argument('-ct', '--cleartext', { action: 'store_true', help: 'print the token in clear text' });
common_arguments(parser);
const args = parser.parse_args(argv);
// get configuration from defaults and/or the environment
const api_config = new ApiConfig({ verbosity: args.log_level, version: args.api_version });
// imports that depend on the version of the API
const { AccessToken } = await import(`../src/${api_config.version}/access_token.js`);
const access_token = new AccessToken(api_config, { name: args.access_token });
access_token.read();
await access_token.refresh();
// Note: It is best practice not to print credentials in clear text.
// Pinterest engineers asked for this capability to make it easier to support partners.
if (args.cleartext) {
console.warn('Please keep clear text tokens secure!');
console.log('clear text access token after refresh:', access_token.access_token);
}
console.log('hashed access token after refresh:', access_token.hashed());
console.log('writing access token');
access_token.write();
}
if (!process.env.TEST_ENV) {
main(process.argv.slice(2));
}