Announcements

Help Wizard

Step 1

NEXT STEP

Start/Resume Playback API return 401

Solved!

Start/Resume Playback API return 401

Hello,

I'm trying to use PUT https://api.spotify.com/v1/me/player/play

to change a song using Axios, and I had provided the token, but I keep getting 401 error of token not provided.

 

Here is the code that I was using:

 

 

 

 

const putRequest = async () => {
            const response = await axios.put(`https://api.spotify.com/v1/me/player/play?device_id=${device_id}`,
            {
                headers: {
                    Accept: 'application/json',
                    Authorization: 'Bearer ' + props.token,
                    'Content-Type': 'application/json'
                },
                data: { uris: [track_uri] }
            })
        }

putRequest();

 

 

 

 

 

When I use the CONSOLE on the developer website and curl to send a put request to my player, it worked.

When I use Axios, it didn't work. I don't know what I did wrong.

Screenshot 2021-12-18 at 9.26.48 PM.png
Screenshot 2021-12-18 at 9.26.36 PM.png
Reply

Accepted Solutions
Marked as solution

Hello,

 

I had figured out the problem. There is a mistake with the Axios PUT call.

This is the correct way:

 

const response = await axios.put(`https://api.spotify.com/v1/me/player/play?device_id=${player_id}`,
            {
                "context_uri": selectedTrack.album.uri,
                "offset": {
                    "position": selectedTrack.track_number
                },
                "position_ms": 0
            },
            {
                headers: {
                    Accept: 'application/json',
                    Authorization: 'Bearer ' + props.token,
                    'Content-Type': 'application/json'
                }
            })

 

 

Thank you so much.

View solution in original post

3 Replies

Hey @zhenyang, thanks for posting here.

 

Hmm, it could be that the token is expired or incorrect. Could you make sure you've requested the right scope(s): user-modify-playback-state? Also, could you double-check if you've added the users to your application in your Spotify for Developers dashboard? Let me know how you get on!

 

Have a great day,

Hubo

HuboSpotify 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.

Hello Hubo, thank you for your reply.

The scope that I'm requesting are:
1. streaming
2. user-read-email
3. user-read-private
4. user-read-playback-state
5. user-modify-playback-state
6. user-read-currently-playing

I'm also added my own Spotify account into Spotify for Developers dashboard.

About the token, I was able to use the token to request a get to the search API 'https://api.spotify.com/v1/search', before and after the PUT https://api.spotify.com/v1/me/player/play.

If the token had expired or incorrect during the PUT https://api.spotify.com/v1/me/player/play request, I should not be able to request a GET https://api.spotify.com/v1/search. But I did get a 200 status code from GET https://api.spotify.com/v1/search request.

I'm still cannot get a 200 status code from PUT https://api.spotify.com/v1/me/player/play request. Feedback from the request still 401 'No token provided'.

Appreciate your help, Hubo
Thank you
Marked as solution

Hello,

 

I had figured out the problem. There is a mistake with the Axios PUT call.

This is the correct way:

 

const response = await axios.put(`https://api.spotify.com/v1/me/player/play?device_id=${player_id}`,
            {
                "context_uri": selectedTrack.album.uri,
                "offset": {
                    "position": selectedTrack.track_number
                },
                "position_ms": 0
            },
            {
                headers: {
                    Accept: 'application/json',
                    Authorization: 'Bearer ' + props.token,
                    'Content-Type': 'application/json'
                }
            })

 

 

Thank you so much.

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