javascriptnode.jsexpressaccess-tokenspotify-app

{"error":"invalid_client"} client credentials error spotify with express


I am developping an api on spotify. I want to retrieve clients credentials. I set up my app on the dashboard. My client_id and secret are correct. But I have the same error at the end when I try to retrieve this client credential: "error":"invalid_client" I look for my problem on web but no one correspond to my problem. Here is my code: `

const express = require("express");
const path = require("path");
const cors = require("cors");
const fetch = (...args) =>
    import('node-fetch').then(({default: fetch}) => fetch(...args));
  
const request = "https://accounts.spotify.com/api/token";

const code = Buffer.from(client_id + ":" + client_secret).toString("base64");

const app = express();
const optionsTOKEN = {
  method: "POST",
  body: "grant_type=client_credentials",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": "Basic" +code
  },
  // json: true,
};

app.get("/code", async (req, res) => {
  const data =   await retrieveCode(request, optionsTOKEN);
  console.log(res.statusCode)
  res.send(data);
});

app.listen(8888, () => {
  console.log("server running on port 8888");
});

async function retrieveCode(URlRequest, options) {
  try {
    const res = await fetch(URlRequest, options);
    console.log(res);
    const data = await res.json();
    console.log("la data vaut" + data);
    return data
  } catch (err) {
    console.log(`L'erreur: ${err}`);
  }
}

`

Thank you for your help

I try to modify the parameters in my options, set up a new project on my dahsboard, change my port. I am expecting to retrieve the access token


Solution

  • You needs to add a space between "Basic" and code

    before

    "Basic" +code
    

    After

    "Basic " +code
    

    #1 With this code

    This full test code with hide client_id and client_secret

    const express = require("express");
    const fetch = (...args) =>
        import('node-fetch').then(({ default: fetch }) => fetch(...args));
    
    const request = "https://accounts.spotify.com/api/token";
    const client_id = "32-your-client-id-7b";
    const client_secret = "ab-your-client_secret-9e";
    const code = Buffer.from(client_id + ":" + client_secret).toString("base64");
    
    const app = express();
    const optionsTOKEN = {
        method: "POST",
        body: "grant_type=client_credentials",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Basic " + code
        },
        // json: true,
    };
    
    app.get("/code", async (req, res) => {
        const data = await retrieveCode(request, optionsTOKEN);
        console.log(res.statusCode)
        res.send(data);
    });
    
    app.listen(8888, () => {
        console.log("server running on port 8888");
    });
    
    async function retrieveCode(URlRequest, options) {
        try {
            const res = await fetch(URlRequest, options);
            console.log(res);
            const data = await res.json();
            console.log("la data vaut" + JSON.stringify(data));
            return data
        } catch (err) {
            console.log(`L'erreur: ${err}`);
        }
    }
    

    #2 Using this dependencies

    package.json for npm install

    {
      "dependencies": {
        "express": "^4.18.2",
        "node-fetch": "^3.3.0"
      }
    }
    

    #3 npm install

    #4 npm start

    #5 access from browser http://localhost:8888/code

    Response in console

    la data vaut{"access_token":"BQCuVhXlpVQGKGxqK-remove-some-string-nX6sQp8uPSYBMh5lsU","token_type":"Bearer","expires_in":3600}
    200
    

    In Browser, enter image description here