Reputation: 1
var http = require('https');
var qs = require('querystring');
var request = require('request');
var appid = appid
var Oauth = Oauth
var Authorization = 'Basic' + ' '+ new Buffer(appid + ':' +
Oauth).toString('base64');
var url = "https://example.com"
var options = {
host: url,
headers:{'Authorization' : Authorization,
"Content-Type": "application/json"}
};
request.post({
url:url,
headers:{'Authorization' : Authorization,
"Content-Type": "application/json"}
},
function(request, response){
console.log(response)
var body = JSON.parse(response["body"])
var token = body["access_token"]
//console.log(token)
return token
});
I want to be able to access the token globally. For example, I want to be able to print it in the line below.
console.log(token)
However, I get undefined. How do I save the token globally?
Note: I do receive the correct token in the earlier steps.
Upvotes: 0
Views: 75
Reputation: 281
Declare token variable globally and define or assign the value to token variable in POST request. And access it globally after that as well.
Below is the code snippet:
var http = require('https');
var qs = require('querystring');
var request = require('request');
var token;
request.post({
url:url,
headers:{'Authorization' : Authorization,
"Content-Type": "application/json"}
},
function(request, response){
console.log(response)
var body = JSON.parse(response["body"])
token = body["access_token"];
});
Upvotes: 1
Reputation: 145
If you want to save it on client site use browser sessionstorage() or localstorage() or you can store it server side which depends on what use
Upvotes: 0