Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

252
Vistas
Spotify API OAUTH2 Server Error Response when requesting token

Spotify Oauth2 Docs - Client Credential Flow

I asked a similar question, and I was able to get the code running in google apps script (javascript). I am using Client Credential Flow. However, I tried to recreate this code in Nodejs. When running this code, I receive a server error.

error: "server_error"

This is the problem code in Nodejs

const defaultConfig = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
// this encodes the app_id and api_key into a base64 string like the documentation states 
    'Authorization': `Basic ${btoa(spotify.API_ID + ':' + spotify.API_KEY)}`
  },

  body: JSON.stringify({
    'grant_type': 'client_credentials',
  })
  };

// this function is wrapped in a list titled, "apiSettings" with other functions. I just included the authorization function here:
  const apiSettings = {
   getSpotifyAuthorizeToken: async () => {
const endpoint = "https://accounts.spotify.com/api/token";
return await (await fetch(endpoint, defaultConfig)).json();
  },
};

I got this similar code to work in google apps script with no problems.

Could the server error be a result of me running Nodejs on a "localhost:xxxx"? The documentation states that, "The Client Credentials flow is used in server-to-server authentication."

if this is not the case, could you provide a working

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Spotify API OAUTH2 Server Error Response when requesting token

From your previous question, I would like to propose the following sample scripts.

Sample script 1:

If you want to convert the script of this answer to Node.js and node-fetch, how about the following script?

const spotify = { API_ID: "API_ID", API_KEY: "API_KEY" }; // Please set your client ID and client secret.

const buf = Buffer.from(spotify.API_ID + ":" + spotify.API_KEY);
const para = new URLSearchParams();
para.append("grant_type", "client_credentials");
const defaultConfig = {
  method: "POST",
  headers: { Authorization: `Basic ${buf.toString("base64")}` },
  body: para,
};
const apiSettings = {
  getSpotifyAuthorizeToken: async () => {
    const endpoint = "https://accounts.spotify.com/api/token";
    return await (await fetch(endpoint, defaultConfig)).json();
  },
};

Sample script 2:

If you want to convert the script of this answer to Node.js and node-fetch, how about the following script?

const spotify = { API_ID: "API_ID", API_KEY: "API_KEY" }; // Please set your client ID and client secret.

const para = new URLSearchParams();
para.append("grant_type", "client_credentials");
para.append("client_id", spotify.API_ID);
para.append("client_secret", spotify.API_KEY);
const defaultConfig = {
  method: "POST",
  body: para,
};
const apiSettings = {
  getSpotifyAuthorizeToken: async () => {
    const endpoint = "https://accounts.spotify.com/api/token";
    return await (await fetch(endpoint, defaultConfig)).json();
  },
};

Reference:

  • node-fetch
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda