Type in your question below and we'll check to see what answers we can find...
Loading article...
Submitting...
If you couldn't find any answers in the previous step then we need to post your question in the community and wait for someone to respond. You'll be notified when that happens.
Simply add some detail to your question and refine the title if needed, choose the relevant category, then post.
Before we can post your question we need you to quickly make an account (or sign in if you already have one).
Don't worry - it's quick and painless! Just click below, and once you're logged in we'll bring you right back here and post your question. We'll remember what you've already typed in so you won't have to do it again.
Please see below the most popular frequently asked questions.
Loading article...
Loading faqs...
Please see below the current ongoing issues which are under investigation.
Loading issue...
Loading ongoing issues...
Plan
Premium Duo
Operating System
Windows 10
My Question or Issue
Hey guys, I've been working on a simple project to use the api to get my current listening information so that I can display it on a localhost website. The issue(s) I have been having is that using me/player/currently-playing returns error 404 (not found), while using most other endpoints I get error 403. I have been able to successfully use some endpoints under /users/ for testing. If I use just me/player I get error 403. If I make a typo I get error 404 (forbidden). . Does anyone have an idea of why this is happening? I saw there was a similar issue a few months back but I assume it is not related to that. Please forgive cluttered/redundant code as I'm not good with javascript and diagnosing errors.
const SpotifyAPI = (function(){ // Spotify dashboard ids var CLIENTID = "myid"; var CLIENTSECRET = "mysecret"; var redirect_url = "http://127.0.0.1:3000/index.html"; // Define the _getToken function const _getToken = async () => { const result = await fetch(`https://accounts.spotify.com/api/token`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic ' + btoa(CLIENTID + ':' + CLIENTSECRET) }, body: 'grant_type=client_credentials&client_id=myid&client_secret=mysecret' }); const data = await result.json(); const accessToken = data.access_token; console.log("Access Token: ", accessToken); return accessToken; }; // Return the _getToken function to be accessible outside return { getToken: _getToken }; })(); //get current song const getCurrentSong = async (accessToken) => { if (!accessToken) { console.error("Failed to obtain access token"); return; } const response = await fetch('https://api.spotify.com/v1/me/player/currently-playing', { method: 'GET', headers: { 'Authorization': 'Bearer ' + accessToken, } }); if (response.status === 204) { console.log("No content: No song is currently playing"); return null; } if (!response.ok) { console.error("Failed to fetch currently playing song:", response.statusText); return null; } const data = await response.json(); console.log(data) return data.item; }; // Example usage SpotifyAPI.getToken() .then(accessToken => { getCurrentSong(accessToken) .then(currentSong => { if (currentSong) { console.log("Currently playing song:", currentSong.name); } else { console.log("No song is currently playing"); } }) .catch(error => console.error("Error fetching current song:", error)); }) .catch(error => console.error("Error fetching access token:", error));
Solved! Go to Solution.
You can store your generated tokens server-side after your first login. A server-side script can use them to get the information you want to display.
Because an access token is only valid for 1 hour, you'll need to write code that can renew that token with the refresh token. You'll get a new refresh token when it is expired. The old stored tokens need to be replaced with the new once.
I'm assuming this is due to me using client credentials. I am rebuilding using PKCE and will update.
Nevermind, I don't want to prompt other people to sign in, I just want only my information accessible
You can store your generated tokens server-side after your first login. A server-side script can use them to get the information you want to display.
Because an access token is only valid for 1 hour, you'll need to write code that can renew that token with the refresh token. You'll get a new refresh token when it is expired. The old stored tokens need to be replaced with the new once.
Thank you for the reply. Since this is such a small project where security does not matter, would it be best for me to just use the Authorization Code Flow and refresh in the backend?
Yes, at the backend it's fine.
Awesome, thanks!
I have the same problem and I think the token must be valid because...
1) It's not even an hour yet, so it can't be overdue
2) I have a service running on the server side that renews the token every 50 minutes and saves it to a file
And I get this response
Error getting information about currently playing song: { "error" : { "status" : 404, "message" : "Invalid username" } }
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…