0

How to refer to a fetched access token (JSON) in the endpoint URI in JavaScript?

Returned JSON Example

{token_type: 'Bearer', expires_at: 1681425284, expires_in: 15892, refresh_token: 'fetched_refresh_token94f5c1f7f241bnh11', access_token: 'fetched_access_token8c13694dd4b34c01f44b51', …}

JavaScript

async function getActivities(res) {
  const fetched_access_token = res.access_token;
  const activities_link = `https://www.strava.com/api/v3/segments/11111111?access_token=????what-to-put-here????`

I need the returned access_token to be referred in the endpoint URI to call the JSON data from the endpoint

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • `${fetched_access_token}`? Am I missing something? – Barmar Apr 18 '23 at 19:01
  • Isn't this the same as your [previous question](https://stackoverflow.com/questions/76008023/how-to-use-a-fetched-access-token-to-fetch-an-endpoint)? – Barmar Apr 18 '23 at 19:02
  • you should put the access token in the header, not in the URL. How access token implemented in API (how does it verify)? – Suraj Neupane Apr 18 '23 at 19:07
  • Does this answer your question? [How to interpolate variables in strings in JavaScript, without concatenation?](https://stackoverflow.com/questions/3304014/how-to-interpolate-variables-in-strings-in-javascript-without-concatenation) – Heretic Monkey Apr 18 '23 at 19:15
  • Related to previous my question indeed, but I still got an error there : "Uncaught (in promise) ReferenceError: access_token is not defined." Apparently the token is not picked up because it is not global: https://stackoverflow.com/questions/40882561/referenceerror-token-is-not-defined So, how do I make it global? – user21633585 Apr 20 '23 at 09:41

1 Answers1

0

Well you have everything in its place

async function getActivities(res) {
  const fetched_access_token = res.access_token;
  const activities_link = `https://www.strava.com/api/v3/segments/11111111?access_token=${fetched_access_token}`
}

use the template strings ${} notation to interpolate the string with the access token in the url

  • I still got the following error : "Uncaught (in promise) ReferenceError: access_token is not defined." Apparently the token is not picked up because it is not global: https://stackoverflow.com/questions/40882561/referenceerror-token-is-not-defined So, how do I make it global here? – user21633585 Apr 20 '23 at 12:31
  • @user21633585 can you just console.log(res) to check whats inside it – Rituparna Warwatkar Apr 20 '23 at 21:08
  • {token_type: 'Bearer', expires_at: 1682001100, expires_in: 21600, refresh_token: 'dc5ce39b0031665a086a79e94f5c1f7f111c2dd0', access_token: '6df6da4381487a094b5a2811a0cf3072676e67ca', …} exchange_token.asp?s…31f5d&scope=read:42 Uncaught (in promise) ReferenceError: access_token is not defined at getActivities (exchange_token.asp?s…5d&scope=read:42:91) at exchange_token.asp?s…f5d&scope=read:34:5 getActivities @ exchange_token.asp?s…31f5d&scope=read:42 (anonymous) @ exchange_token.asp?s…31f5d&scope=read:34 Promise.then (async) (anonymous) @ exchange_token.asp?s…31f5d&scope=read:32 – user21633585 Apr 22 '23 at 07:32
  • Sorry I couldn't quite follow, can you post a screenshot? – Rituparna Warwatkar Apr 22 '23 at 07:34