-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh-tokens.js
71 lines (56 loc) · 2.07 KB
/
refresh-tokens.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const axios = require('axios');
const qs = require('qs');
const fs = require('fs');
const tokenOutputPath = '/output/token.json';
const CONSUMER_KEY = process.env.CONSUMER_KEY;
if (!CONSUMER_KEY) {
throw new Error('CONSUMER_KEY is not defined');
}
async function refreshAccessAndRefreshTokens(refreshToken) {
const data = qs.stringify({
'grant_type': 'refresh_token',
'refresh_token': refreshToken,
'client_id': CONSUMER_KEY,
'access_type': 'offline',
});
const config = {
method: 'post',
url: 'https://api.tdameritrade.com/v1/oauth2/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: data
};
const response = await axios(config);
return response.data;
}
function mergeAndEnrichTokens(token, newToken) {
return {
...token,
...newToken,
// overwrite only if the new token has the expiry fields too
expires_at: newToken.expires_in ? Date.now() + newToken.expires_in * 1000 : token.expires_at,
refresh_token_expires_at: newToken.refresh_token_expires_in ? Date.now() + newToken.refresh_token_expires_in * 1000 : token.refresh_token_expires_at,
expires_in: undefined,
refresh_token_expires_in: undefined,
};
}
async function refreshAndEnhanceToken(token) {
const newAccessToken = await refreshAccessAndRefreshTokens(token.refresh_token);
return mergeAndEnrichTokens(token, newAccessToken);
}
async function main() {
let token = JSON.parse(fs.readFileSync(tokenOutputPath).toString());
const { refresh_token_expires_at } = token;
const now = Date.now();
if (now > refresh_token_expires_at) {
console.warn("Refresh token appears to have expired, may be unable to refresh");
}
console.log("Refreshing access and refresh tokens...");
token = await refreshAndEnhanceToken(token);
fs.writeFileSync(tokenOutputPath, JSON.stringify(token, null, 2));
}
main().catch((e) => {
console.error(e.response.status, e.response.data);
process.exit(1);
});