title: Spotify API - Getting 406 Unacceptable On Token Request When Using HttpRequestMessage
body: <p>In my requests to Spotify's "get token" endpoint using .NET's HttpRequestMessage, I'm getting a 406 (Unacceptable) response.</p>
  
  <p>Here's my post method (fyi, this is not optimized code -- more of a proof of concept):</p>
  
  <pre><code>        public static async Task&lt;string&gt; PostForm(HttpClient httpClient, string url, List&lt;KeyValuePair&lt;string, string&gt;&gt; nameValueList)
          {
              using (var httpRequest = new HttpRequestMessage(HttpMethod.Post, url))
              {
                  httpRequest.Content = new FormUrlEncodedContent(nameValueList);
                  using (var response = await httpClient.SendAsync(httpRequest))
                  {
                      response.EnsureSuccessStatusCode();
                      return await response.Content.ReadAsStringAsync();
                  }
              }
          }
  </code></pre>
  
  <p>I'm calling that method with this:</p>
  
  <pre><code>        public Token GetSpotifyToken()
          {
              var httpClient= new HttpClient();
              httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
              httpClient.DefaultRequestHeaders.Add("Authorization", "Basic MyApiCredsBase64EncodedYesIveConfirmedTheseWork=");
  
              var url = "https://accounts.spotify.com/api/token";
              var grantType = new KeyValuePair&lt;string, string&gt;("grant_type", "client_credentials");
  
              return JsonConvert.DeserializeObject&lt;Token&gt;
                  (ApiRequest.PostForm(postClient, url,
                  new List&lt;KeyValuePair&lt;string, string&gt;&gt;() { grantType } )
                  .GetAwaiter()
                  .GetResult());
          }
  </code></pre>
  
  <p>I know that the issue is related to how <code>grant_type=client_credentials</code> is getting added to the request. This method, which uses RestSharp, works fine:</p>
  
  <pre><code>        public Token GetSpotifyToken()
          {
              var client = new RestClient("https://accounts.spotify.com/api/token");
              var request = new RestRequest(Method.POST);
              request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
              request.AddHeader("Authorization", "Basic MyApiCredentialsBase64Encoded=");
              request.AddParameter("undefined", "grant_type=client_credentials", ParameterType.RequestBody);
              IRestResponse response = client.Execute(request);
              return JsonConvert.DeserializeObject&lt;Token&gt;(response.Content);
          }
  </code></pre>
  
  <p>But I've tried adding <code>grant_type=client_credentials</code> to the HttpRequestMessage object in a few different ways, without success.</p>