Spotify API Get Recently Played Track for Multiple Users
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page