Announcements

Help Wizard

Step 1

NEXT STEP

"Invalid username" when trying to list devices

"Invalid username" when trying to list devices

Premium plan, using client credentials auth as this is the Node backend of an app hitting the API. Devices are Windows 10 laptop and tablet. Country is US.

 

Trying to write a Node script to play music on a certain device. But I can't get devices to list. I get that "invalid username" error. Here's my code:

 

 

 

const auth = Buffer.from(`${config.spotify_client_id}:${config.spotify_client_secret}`).toString('base64');
const options = {
    method: 'POST',
    url: `https://accounts.spotify.com/api/token?username=(my id here)`,
    headers: {
        Authorization: `Basic ${auth}`,
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    form: {
        grant_type: 'client_credentials',
        scope: "user-modify-playback-state user-read-playback-state"
    }
}
request(options, (err, resp, body) => {
    const accessToken = JSON.parse(body).access_token;
    const getDevicesOptions = {
        method: 'GET',
        url: 'https://api.spotify.com/v1/me/player/devices',
        headers: {
            Authorization: `Bearer ${accessToken}`,
            Accept: 'application/json',
            'Content-Type': 'application/json'
        }
    }
    request(getDevicesOptions, (devErr, devResp, devBody) => {
         // .... never gets past here
    });
});

 

 

 

As far as I can tell the first request always works correctly. I get a valid access_token, although I wonder if the problem is the scopes being incorrectly set. I've tried some variations but never worked. Either continued to say invalid username, or invalid access token.

 

In the second request, the value of devBody is 

 

 

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

 

 

 I tried adding ?username=(my numeric user id) to the end of /player/devices, to no avail.

Reply
2 Replies

I have exactly the same problem (in Java). Did you solve it?

The Client Credentials flow is used in server-to-server authentication. Only endpoints that do not a... 

 

But how to play music using Spotify Connect and an custom app without Web-UI? I evaluate to build a device for my little son, that is controlled by NFC tags.

Suggested posts