Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

{"error":{"status":404,"message":"Invalid username"}} Error (Nodejs)

{"error":{"status":404,"message":"Invalid username"}} Error (Nodejs)

Plan

Free/Premium

 

Country

Turkey

 

Language

Nodejs

 

My Question or Issue

 

From this code:

 

 

 

 

 

const express = require('express')
const app = express()

var client_id = 'asd';
var client_secret = 'asd';

// Base64 encoding of the client_id and client_secret
var encoded = btoa(client_id + ':' + client_secret);

// Fetch request to Spotify API
fetch('https://accounts.spotify.com/api/token', {
  method: 'POST',
  headers: {
    'Authorization': 'Basic ' + encoded,
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: 'grant_type=client_credentials'
})
.then(response => response.json())
.then(data => {
  var access_token = data.access_token;

  //dinlenen
  fetch('https://api.spotify.com/v1/me/player/currently-playing', {
    headers: {
      'Authorization': 'Bearer ' + access_token
    }
  })
  .then(response => response.json())
  .then(asd => {
    console.log(asd);
    //siteye çevir

    app.get('/', function (req, res) {
      res.send(asd)
    })
  })
  .catch(error => console.log(error));
}).catch(error => console.log(error));

app.listen(3000, () => {
  console.log('Server started on port 3000');
});

 

 

 

 

 

 

I am getting this output:

{ error: { status: 404, message: 'Invalid username' } }

 

 

 

What mistake am I making and how can I fix it?

Reply
3 Replies

The mistake is that you are trying to use Client Credentials with an endpoint that requires Authorization.

Since the Client Credentials flow does not include authorization, only endpoints that do not access user information can be accessed.

So, you should use the Authorization Code Flow.

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

How will I do? If possible, is there any chance you can edit the request? 

To modify the code to work with the Authorization Code Flow, you need to make several changes.

An easier way is to use Spotify Web API Node.

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

Suggested posts

Let's introduce ourselves!

Hey there you,   Yeah, you! 😁   Welcome - we're glad you joined the Spotify Community!   While you here, let's have a fun game and get…

ModeratorStaff / Moderator/ 4 years ago  in Social & Random