Announcements

Help Wizard

Step 1

NEXT STEP

Call to https://accounts.spotify.com/api/token no longer working

Call to https://accounts.spotify.com/api/token no longer working

Plan

Premium

Country

United States

 

My Question or Issue

Using the following code:

 

 

 

using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{clientId}:{clientSecret}")));

var formData = new Dictionary<string, string>
{
{ "grant_type", "client_credentials" }
};

var response = await client.PostAsync("https://accounts.spotify.com/api/token", new FormUrlEncodedContent(formData)); // FAILS HERE

var content = await response.Content.ReadAsStringAsync();

 

 

 

I can no longer get an access token to make other API calls... This just stopped working out of nowhere with no code changes, and the application just stops when it tries to execute the marked point in the code. I also attempted to wrap that part of the code in a try/catch and still no helpful exceptions (I assume due to the async nature?)... Any thoughts? I was running different versions of my code back and forth to try and figure out why one version was working and one wasn't, but then when the version that had been working this whole time stopped working... Started to scratch my head. Any thoughts?

Reply
1 Reply

You are missing the ContentType header.

The following code should work:

 

using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{clientId}:{clientSecret}")));

var formData = new Dictionary<string, string>
{
{ "grant_type", "client_credentials" }
};

var content = new FormUrlEncodedContent(formData);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");

var response = await client.PostAsync("https://accounts.spotify.com/api/token", content);

var responseContent = await response.Content.ReadAsStringAsync();

Suggested posts