title: How to call the Spotify API from C#
body: <p>I'm trying to call the spotify API with C#.
  Unfortunately, I'm already stuck with getting the <code>access token</code></p>
  
  <p>This is how I tried to get it:</p>
  
  <pre><code>private static async Task&lt;string&gt; GetAccessToken()
          {
              SpotifyToken token = new SpotifyToken();
  
              string postString = string.Format("grant_type=client_credentials");
              byte[] byteArray = Encoding.UTF8.GetBytes(postString);
  
              string url = "https://accounts.spotify.com/api/token";
  
              WebRequest request = WebRequest.Create(url);
              request.Method = "POST";
              request.Headers.Add("Authorization", "Basic {Encoded myClientIdXXX:myAppSecretYYY}");
              request.ContentType = "application/x-www-form-urlencoded";
              request.ContentLength = byteArray.Length;
              using (Stream dataStream = request.GetRequestStream())
              {
                  dataStream.Write(byteArray, 0, byteArray.Length);
                  using (WebResponse response = await request.GetResponseAsync())
                  {
                      using (Stream responseStream = response.GetResponseStream())
                      {
                          using (StreamReader reader = new StreamReader(responseStream))
                          {
                              string responseFromServer = reader.ReadToEnd();
                              token = JsonConvert.DeserializeObject&lt;SpotifyToken&gt;(responseFromServer);
                          }
                      }
                  }
              }
              return token.access_token;
          }
  </code></pre>
  
  <p>But I get an error:</p>
  
  <blockquote>
    <p>Der Remoteserver hat einen Fehler zurückgegeben: (400) Ungültige Anforderung.</p>
  </blockquote>
  
  <p>Which translates to:</p>
  
  <blockquote>
    <p>The remote server returned an error: (400) Bad Request.</p>
  </blockquote>
  
  <p>What am I doing wrong here?
  I've registered my app at spotify and set the clientId and secret in the request header...</p>
  
  <p>Thanks in advance</p>