Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!