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

Get currently playing track API - Getting a 400 & 404 error

Solved!

Get currently playing track API - Getting a 400 & 404 error

Premium- Family Plan

United Arab Emirates

 

I am trying to use the Get currently playing track API and I am getting 400 & 404  error "Only valid bearer authentication supported" & "Invalid Username" , I am able to use the Search API which works perfectly fine!

 

 

 

async function getNowPlaying(){
    const nowPlayingParameters = {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${accessToken}`
      }
    }

    const res = await fetch(`https://api.spotify.com/v1/me/player/currently-playing?market=AE&additional_types=track`, nowPlayingParameters)

    const json = await res.json()
    console.log(json)
}

 

 

 

Screenshot 2023-05-14 at 2.53.18 AM.png
Reply

Accepted Solutions
Marked as solution

How do you get an access token? To use that endpoint, you'll need to get an access token through the Authorization Code Flow or the Authorization Code with PKCE Flow, and using the Scopes user-read-playback-state and user-read-currently-playing.

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.

View solution in original post

3 Replies
Marked as solution

How do you get an access token? To use that endpoint, you'll need to get an access token through the Authorization Code Flow or the Authorization Code with PKCE Flow, and using the Scopes user-read-playback-state and user-read-currently-playing.

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.

I was using the fetch API to make a request. I am trying to create a widget that shows my currently playing track. As I understand Spotify recommends using  Authorization Code with PKCE Flow for SPA's, My question would be how can I make this a one time authorization without making additional requests?.

Sorry I am still a beginner, and your help is very much appreciated, thank you for your patience! 😊

To achieve your intended goal, you must use the Authorization Code Flow as it is the only flow that supports refresh tokens. You should create a server-side script that can retrieve your currently playing track using the relevant endpoints. To do this, your script should use the access token that you've stored in a dynamic database on the server-side.

When you request an access token, you will also receive a refresh token that your script can use to refresh the access token when it has expired.

On the client-side, you should configure requests to be sent every twenty seconds to retrieve the currently playing track data from the server-side script.

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