0

I am trying to get saved tracks from Spotify, what am I doing wrong?

        using (HttpClient client = new HttpClient())
        {              
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            client.DefaultRequestHeaders.Add("Content-Type", "application/json");

            HttpResponseMessage resp = await client.GetAsync("https://api.spotify.com/v1/me/tracks");

            string responseBody = await resp.Content.ReadAsStringAsync();

            Console.WriteLine(responseBody);
        }

This is a request sample from the documentation:

curl --request GET
--url https://api.spotify.com/v1/me/tracks
--header 'Authorization: '
--header 'Content-Type: application/json'

Lost_
  • 9
  • 1
  • 4
  • 2
    Why do you think it's wrong? In what way does this fail? – David Mar 15 '22 at 21:30
  • Hello, David! The mistake is: "Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects." – Lost_ Mar 15 '22 at 21:36
  • 1
    Might be a dupe of [this one](https://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request) – Mike Christensen Mar 15 '22 at 21:39
  • 1
    Maybe it doesn't like the `Content-Type` header? After all, a GET request doesn't *have* "content". Try without that header, or with sending an `Accepts` header of `application/json`. It's possible this was an oversight in the API docs, or some nebulous interpretation of standards between the vendors. – David Mar 15 '22 at 21:40
  • 1
    Yea I'd probably just remove `client.DefaultRequestHeaders.Add("Content-Type", "application/json");` - I don't think it actually does anything since you're not POST'ing content. – Mike Christensen Mar 15 '22 at 21:41
  • Tried without content type - returns this array: " "error" : { "status" : 403, "message" : "Insufficient client scope" } }". – Lost_ Mar 15 '22 at 21:44
  • 1
    I think that's a new problem. Seems like maybe the OAuth token is bad? – Mike Christensen Mar 15 '22 at 21:45
  • Same error with Accepts :( – Lost_ Mar 15 '22 at 21:45
  • Checked token again. – Lost_ Mar 15 '22 at 21:52
  • I'm not an OAuth expert, but I think sometimes when you request a token you have to request certain scopes.. I think you're doing something wrong when you get your token, but we can't see that part of the code.. – Mike Christensen Mar 15 '22 at 22:00

0 Answers0