Team, below is a part of the code I'm failing to correctly pass the api response body "access_token", to the outer function so I can use it later with subsequent calls. can you please help?in the res.on('end' ... , I'm assigning the variable at the content extracted from the response body.however, when I return back the variable at to the outside, it's always empty as defined on the first line of the function code.function genAccessToken() {var at = '';var httpUri = '/oauth2/v6/tokens';var httpMethod = 'POST';var httpOptions = {'method': httpMethod,'hostname': hostname,'path': httpUri,'headers': {'Authorization': basicAuth},'maxRedirects': 20};var ehtsKeyValueMap = new Map();ehtsKeyValueMap.set('Authorization', basicAuth);ehtsKeyValueMap.set('uri', httpUri);ehtsKeyValueMap.set('http-method', httpMethod);// add an http header with the generated POP tokenhttpOptions.headers['X-Authorization'] = popTokenBuilderUtil.buildPopToken(ehtsKeyValueMap, privateKeyPemStr);console.log(httpOptions);// execute request for access tokenvar strB = '';const req = https.request(httpOptions, (res) => {console.log(\statusCode: ${res.statusCode}\);res.on('data', function (chunk) { strB += chunk; })res.on('end', function () { at = JSON.parse(strB).access_token; })})req.on('error', error => {console.error(error)})req.end();return at;}var accessToken = genAccessToken();console.log(accessToken);
Submitted August 19, 2020 at 12:03AM by xantegh
No comments:
Post a Comment