1

I am trying to a request a spotify access token using the procedure defined under 'Client Credentials Flow' on the Spotify developer page,

Here is my current implementation (Currently returns http 400 error):

using (var client = new HttpClient())
{
    var requestBody = new StringContent(
        "grant_type:client_credentials", 
        Encoding.UTF8, 
        "application/x-www-form-urlencoded");

    var requestEncrypted = Convert.ToBase64String(
        Encoding.UTF8.GetBytes(ClientId + ":" + ClientSecret));

    client.DefaultRequestHeaders.Add(
         "Authorization", 
         $"Basic {requestEncrypted}");

    var tokenResult = await client.PostAsync(TokenEndpoint, requestBody); 
}

I have tried other ways for formatting the request body including as json (Note: changing the encoding type to 'application/json' results in a http 415 error (media not supported)

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
Tom Gothorp
  • 93
  • 1
  • 8

1 Answers1

3

For one, you need to use equals not a colon.

grant_type=client_credentials

Here is an example POST from the RFC.

POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials
Community
  • 1
  • 1
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467