Plan
Free/Premium
Country
US
My Question or Issue
I am trying to make a project that will get the BPMs of a list of songs, and then output them to the user, as well as including some sorting functionality. I am trying to use Spotify to get the BPM of the songs, and then handle the rest myself. My app was briefly working, and since then I have been getting 403 Forbidden errors on all of my requests (except for getting an access token). I am using C# as I am working inside of Unity, and my code for requesting the song BPM looks like this:
public IEnumerator FetchBPMForSong(string trackId, Action<float> onBPMReceived)
{
if (string.IsNullOrEmpty(accessToken))
{
yield return GetAccessToken();
}
string featuresUrl = $"https://api.spotify.com/v1/audio-features/{trackId}";
UnityWebRequest request = UnityWebRequest.Get(featuresUrl);
request.SetRequestHeader("Authorization", "Bearer " + accessToken);
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
var json = JObject.Parse(request.downloadHandler.text);
float bpm = json["tempo"]?.ToObject<float>() ?? 0f;
Debug.Log($"BPM for Track {trackId}: {bpm}");
onBPMReceived?.Invoke(bpm);
}
else
{
Debug.LogError($"Error fetching BPM: {request.error}\nResponse: {request.downloadHandler.text}");
}
}
and my code for getting an access token looks like this:
private IEnumerator GetAccessToken()
{
string authUrl = "https://accounts.spotify.com/api/token";
string credentials = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes($"{ClientId}:{ClientSecret}"));
WWWForm form = new WWWForm();
form.AddField("grant_type", "client_credentials");
UnityWebRequest request = UnityWebRequest.Post(authUrl, form);
request.SetRequestHeader("Authorization", "Basic " + credentials);
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
var json = JObject.Parse(request.downloadHandler.text);
accessToken = json["access_token"]?.ToString();
Debug.Log($"Access Token Retrieved: {accessToken}");
}
else
{
Debug.LogError($"Error fetching access token: {request.error}\nResponse: {request.downloadHandler.text}");
}
}
I'm unsure what causes the issue to start appearing and would appreciate any feedback.