0

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 ,
    }
};
InvaderZim
  • 19
  • 4
  • It should work. Can you post a [mcve]? You're probably calling `getCode` too early. Or you're overwriting the Access token with `undefined`. – jabaa May 13 '22 at 09:30
  • I'd post something reproduceable but I'm not sure how without the API authorization and credentials to get an access token in response. I'm calling getCode() in a different file in hopes of getting the token for use but I only get 'undefined'. Where would I be overwriting it? I only set it once where you see `setCode(accessToken)` – InvaderZim May 13 '22 at 09:35
  • You can either mock the API calls or at least show how you call `getCode`. Technically, your approach shouldn't be a problem. – jabaa May 13 '22 at 09:38
  • Please have a look at my edit, that is where I'm calling `getCode`. – InvaderZim May 13 '22 at 09:44
  • `getCode()` in `event.js` is run immediately after the import. `setCode` waits for the response. How do you time it, that `getCode` is called after `setCode`? – jabaa May 13 '22 at 09:46
  • `var accessToken = getCode();` runs when events.js is initialised, which is before the HTTP request has completed and called `setCode`. See the duplicate. You probably want to assign a promise and not the access code itself. (And also stop using `request` as per the message at the top of its NPM documentation). – Quentin May 13 '22 at 09:46
  • Your problem is not scope. Your problem is time. Your HTTP request will complete **after** your outside code executes – slebetman May 13 '22 at 09:50
  • thank you for the comments, I'll have a read of the duplicate right now. Excuse me if I sound like a noob but if I execute `getAccessCode.js` first and then execute `events.js`, should that not first call `setCode` and set the access token and then when `events.js` is executed, `getCode` is called? I thought this order of operations would set the token before I call it for use. – InvaderZim May 13 '22 at 09:56
  • You don't call modules. You import them. An import will evaluate the synchronous code, but there is asynchronous, too. This code runs concurrently and can start after the second file is evaluated. It's probably even guaranteed that the asynchronous code starts after all synchronous code has finished. I don't know the details for imports. – jabaa May 13 '22 at 10:02

0 Answers0