简体   繁体   中英

Spotify API Client Credentials returns "error": "invalid_client"

I'm getting an error back on my server.

{ "error": "invalid_client" }

I've encoded to base64 and whitelisted the domain, still getting the same error. I would appreciate any help, this is driving me crazy lol.

import express from 'express'
import fetch from 'node-fetch'

const app = express()

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

const options = {
  method: "POST",
  body: "grant_type=client_credentials",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": "Basic" + encodedPayload
  },
  json: true
};

app.get('/', async (req, res) => {
    const data = await getToken();
    console.log(data);
    res.json(data);
})


async function getToken(){
  const res = await fetch("https://accounts.spotify.com/api/token", options)
  const data = await res.json()
  return data;
}

app.listen(3000)

The solution was mentioned in a comment by rob_med :

There is currently no spacing between "Basic" and the payload in the "Authorization" header

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM