I am working with spotify api trying to get my access token. I am trying to store it in a var so I can you it later (here I am using async and await). For some reason it is not working. I am a beginner so maybe its something small but right now, it is going over my head and I need help. Thank you guys in advanced
I have tried working with fetch to do this but I need to promise to be resolved before I can store in a var, so I started to use async and await. I am trying to do that but when I call the await keyword on the function for it to wait for the result, I get the error "await is only valid in async functions and the top level bodies of modules" even though my function is async. Here is the code
let accessToken = async() => {
const response = await fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
'Authorization': "Basic " + btoa(clientId + ":" + clientSecret),
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: 'grant_type=client_credentials'
})
const data = await response.json()
return data.access_token
}
const token = await accessToken();