getAccessCode.js
import { stringify, unescape } from "querystring";
import { request } from "https";
import {getCode, setCode, getRefresh, setRefresh} from './helper.js';
let accessToken = null;
let refreshToken = null;
var resultString;
var options = {
"method": "POST",
"hostname": "sandbox-api.dexcom.com",
"port": null,
"path": "/v2/oauth2/token",
"headers": {
"content-type": "application/x-www-form-urlencoded",
"cache-control": "no-cache"
}
};
var req = request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
resultString = body.toString();
var codes = JSON.parse(resultString);
//console.log(codes)
for(var i in codes){
if(i == 'access_token')
{
accessToken = codes[i];
}
//if(i == 'refresh_token')
//{
//refreshToken = codes[i]
//}
}
setCode(accessToken);
//can get the access token here but nowhere outside this function
console.log("get code: ", getCode());
});
});
req.write(stringify({ client_secret: 'my client secret',
client_id: 'my client ID',
refresh_token: 'my refresh token',
grant_type: 'refresh_token',
redirect_uri: 'my redirect uri' },null,null,{encodeURIComponent:unescape}));
req.end();
I've connected to the Dexcom API (an API to get CGM data) to receive an access token which allows me to access their other endpoints for different CGM data/stats, the token is displayed as console.log output originally, I've edited the code from the API docs to console.log output the data in JSON format so its easier to extract the token. You need this massive access token when making requests to the other endpoints which I have in different files.
The issue is that the access token expires every two hours and I set up global getters and setters in a helper.js file to allow me to set the global access token and use it with the get function in other files as to not manually copy/paste it from the console log in each file every two hours. When I called the 'getCode()' function anywhere outside the scope of the 'res' functions, it does not have any data in it, returns 'undefined' instead of the assess token but it works perfectly fine inside the function.
Is there any way I can use my getCode() function to actually get the token for use in other files? Why does 'setCode()' not have the access token set anymore after the request is over? I'm quite new to node.js so I'm not sure what I need to change to make this happen.
Edit - Here is where I'm calling getCode()
and receiving undefined
. I need to add the access token in the "authorization"
section.
events.js
import {getCode, setCode, getRefresh, setRefresh} from './helper.js';
var accessToken = getCode();
console.log(accessToken); //returns 'undefined'
var resultString
var options = {
"method": "GET",
"hostname": "sandbox-api.dexcom.com",
"port": null,
"path": "/v2/users/self/calibrations?startDate=2019-12-24T04:00:07&endDate=2020-02-09T14:46:39",
"headers": {
"authorization": "Bearer " + accessToken ,
}
};