Spotify WebAPI authorization - invalid credential flow client invalid_client

A direct question, I hope, with a direct answer. I am trying to implement a client credential stream through Node.js using a request. Here is my code

var request = require('request');
var payload = config.spotify.clientID + ":" + config.spotify.clientSecret;
var encodedPayload = new Buffer(payload).toString("base64");

var opts = {
    url: "https://accounts.spotify.com/api/token",
    method: "POST",
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": "Bearer " + encodedPayload
    },
    body: "grant_type=client_credentials&scope=playlist-modify-public playlist-modify-private"
};

request(opts, function (err, res, body) {
    console.log('error', err);
    console.log('status', res.statusCode);
    console.log('body', body);
});

No matter what I do, the response body is always

{"error":"invalid_client"}

I tried making a request using curl with the same result.

$ curl -X POST -H 'Authorization: Bearer <base64encoded client_id:client_secret>' -d 'grant_type=client_credentials&scope=playlist-modify-public playlist-modify-private' https://accounts.spotify.com/api/token

This means that this is a credential issue. I definitely use the correct clientID and clientSecret for my application, which leads me to believe that this is the encoding causing the problem.

Am I doing the right coding? If so, what else could be the reason?

+4
source share
1 answer

Replace

"AUthorization": "Bearer " + ...

with

"Authorization": "Basic " + ...

and see if it works better.

+7

All Articles