0

I am using the code below to retrieve a refresh_token. When I run it, I get the token in the console but if I want to return something instead, I get nothing. Basically I cannot use this code to generate a token that can then be use in another piece of code. What am I missing?

var request = require("request");

var options = {
  method: 'POST',
  url: 'https://YOUR_DOMAIN/oauth/token',
  headers: {'content-type': 'application/x-www-form-urlencoded'},
  form: {
    grant_type: 'refresh_token',
    client_id: 'YOUR_CLIENT_ID',
    client_secret: 'YOUR_CLIENT_SECRET',
    refresh_token: 'YOUR_REFRESH_TOKEN'
  }
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});
Adrian Sultu
  • 330
  • 5
  • 16
  • above code, your console displayed token ? – linthertoss May 30 '20 at 14:12
  • Yes, the console displays the token if i do console.log(body). But i want to instead get that token, and use it somewhere else. I am not sure how. – Adrian Sultu May 30 '20 at 14:14
  • i got it, do you know asynchronous ? – linthertoss May 30 '20 at 14:17
  • A bit, i am still new to this. – Adrian Sultu May 30 '20 at 14:20
  • ok, i'll help you – linthertoss May 30 '20 at 14:20
  • Look at [the documentation for request](https://www.npmjs.com/package/request). It is deprecated. You shouldn't use it at all. – Quentin May 30 '20 at 14:20
  • @Quentin I saw, but i can't find any other method that works for that specific api. Do you have any suggestion that would be equivalent to the above post? – Adrian Sultu May 30 '20 at 14:24
  • You can make a request like that with any of the common libraries for making HTTP requests. – Quentin May 30 '20 at 14:25
  • i resolved my issue with the following code: `async function getAccessToken() { const fs = require("fs"); const date = new Date(); date.setDate(date.getDate() - 2); const axios = require("axios"); const oauth = require("axios-oauth-client"); const getRefreshToken = oauth.client(axios.create(), { url: "URL", grant_type: "refresh_token", client_id: "ID", client_secret: "secret", refresh_token: "token", scope: "scope", }); const auth = await getRefreshToken();` – Adrian Sultu May 30 '20 at 18:46

0 Answers0