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

Spotify API Get Recently Played Track for Multiple Users

Spotify API Get Recently Played Track for Multiple Users

 

Plan

Free

Country

 

Device

All Devices 

Operating System

All OS

My Question or Issue

 

I'm building a web app that will display most popular song for my user base. In Spotify API docs I call Get Recently Played Tracks

It works fine for the first users however when I'm looping to the second (third and so on) I get a 504 error. I've checked Spotify forms and looked for other recommendations but unfortunately I've not seen another way without looping through each users and calling the Get Recently Played Tracks.

I also tried spacing out my calls to Spotify (every 1 min, 5min, etc.) with no luck.

 

 

 

private async Task<SpotifyRecentlyPlayedModel> GetSpotifyRecentlyPlayedData(DateTime afterTime, string AccessToken, long SpotifyUserMappingId) {

 

long afterTimeTimestamp = new DateTimeOffset(afterTime).ToUnixTimeMilliseconds();

//await Task.Delay(1000);

string requestURI = $"https://api.spotify.com/v1/me/player/recently-played?after={afterTimeTimestamp}";

 

using (HttpClient client = new())

{

using HttpRequestMessage request = new(HttpMethod.Get, requestURI);

request.Headers.Clear();

request.Headers.Add("ContentType", "application/json");

request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);

 

using HttpResponseMessage response = await client.SendAsync(request);

if (response.IsSuccessStatusCode) {

string apiResponse = await response.Content.ReadAsStringAsync();

SpotifyRecentlyPlayedModel result = JsonConvert.DeserializeObject<SpotifyRecentlyPlayedModel>(apiResponse);

 

return result;

} else {

= await spotifyDataErrorLogRepository.AddAsync(new() {

SpotifyUserMappingId = SpotifyUserMappingId,

ErrorCode = (int)response.StatusCode,

ErrorMessage = response.ReasonPhrase });

}

}

return null; 

Reply
7 Replies

You'll need the tokens of every user in your user base, and use them in a for each loop.

If your app is still in development mode, it has some limitations.

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.

Thank you for the reply Ximzend. 

Currently I'm using tokens for each users and regenerating them if it's older than 50 minutes. 

 

I will try to switch the mode from development mode to see if there is something there that is blocking me.

It's not a simple switch. As you can read on that page, you will need a quota extension if you don't have it already.

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 agree it is in depth, I've started the process and hope to complete it soon.

I will let you know after I run a test with live cred if it resolved the issue. 

Yeah we have the same issue getting 504s for subsequent calls to fetch recently-played items. We're trying to request a quota extension to see if that fixes it, too.

I'm also getting a 504 error for multiple users when fetching from recently played:

https://github.com/Yooooomi/your_spotify/issues/260

 

Has anyone resolved this yet?

Just wanted to check if you've made any progress with the application? I'm having the same issue and think it's a bit ridiculous that I have to go through an entire process and wait 6 weeks + just to get my friend's recently played tracks haha.

Suggested posts

Type a product name