Using Spotify Documentation for Client Credential Flow here:
I was able to create a API request in google app script (Javascript).
function callAPI () {
SPOTIFY_CLIENT_SECRET = secret
SPOTIFY_CLIENT_ID = id
const HEADERS = {
"Content-Type": "application/json",
'Authorization': `Basic ${Utilities.base64Encode(SPOTIFY_CLIENT_ID + ':' + SPOTIFY_CLIENT_SECRET)})`
}
const BODY = {
'grant_type': 'client_credentials'
}
var url = `https://api.spotify.com/api/token`
var requestOptions = {
'method': 'POST',
'headers': HEADERS,
'payload': JSON.stringify(BODY),
'muteHttpExceptions': true,
'redirect': 'follow'
};
var response = UrlFetchApp.fetch(url, requestOptions);
var data = JSON.parse(response.getContentText());
I am confused about two things, please be able to answer both.
1). The Spotify documentation states to enter "Basic" before the client credentials in the authorization header.
Yet, when I run this code, I get this error
{ error:
{ status: 400,
message: 'Only valid bearer authentication supported' } }
If, I'm am using client credential flow, why does it think I am using a bearer token? (Also if I change authentication to Bearer I get a 401 error "Invalid access token")
2). Could you provide an example of a working version of this code and why it was able to run opposed to mine?