-1

From the 29th May 2017 all calls will require an access token on Spotify. I had developed a web C# application which could get music track information successfully.

Uri uri = new Uri(@"https://api.spotify.com/v1/search?q=" + txtartist.Text + "%20" + txttrack.Text + "&type=track");
WebRequest webRequest = WebRequest.Create(uri); 
WebResponse response = webRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());

But after the recent change, it encounters the error 401 (unauthorized access). How can I add a code to the above code so that the authentication can be done before getting the information?

I think it is possible to create a webrequest object from only one URI, while two URIs is needed (one for authentication and another for search) and I need one integrated code.

P.S.: According to the reference one solution would be:

curl -X GET "https://api.spotify.com/v1/search?q=tania%20bowra&type=artist" -H "Authorization: Bearer {your access token}"

but I do not know how to use the first text after GET (between the quotations) and the second text after -H in the same time for a webrequest object.

Thanks in advance.

Cleptus
  • 3,446
  • 4
  • 28
  • 34
  • H is for Header. – Dave S Jun 12 '17 at 21:26
  • You should add the Token as a authorization header, btw, other questions have the answer to this, like this one https://stackoverflow.com/questions/21158298/how-to-force-webrequest-to-send-authorization-header-during-post – Cleptus Jun 13 '17 at 07:48

2 Answers2

0

Finally I could solve the problem. I had to set "*" for the field "Query" and "artist" for "ItemType" in the endpoint:

https://developer.spotify.com/web-api/console/get-search-item/

then generate the Access Token and use it. Both my previous code and your suggested code work properly. In fact, my Access Token had a problem.

0

The access token is expired after one hour and the application does not work. This method could help me in gathering access token from Spotify automatically, when needed:

public string GetClientCredentialsAuthToken()
    {
        var webClient = new WebClient();

        var postparams = new NameValueCollection();
        postparams.Add("grant_type", "client_credentials");

        var authHeader = Convert.ToBase64String(Encoding.Default.GetBytes("95da9f399a78...:5a7aaef94..."));   //including clientID and Client Secret
        webClient.Headers.Add(HttpRequestHeader.Authorization, "Basic " + authHeader);

        var tokenResponse = webClient.UploadValues("https://accounts.spotify.com/api/token", postparams);

        var TokenRes = Encoding.UTF8.GetString(tokenResponse);

        int pFrom = TokenRes.IndexOf("access_token") + "access_token".Length;
        int pTo = TokenRes.LastIndexOf("token_type");
        string semiToken = TokenRes.Substring(pFrom, pTo - pFrom);
        string myAccessToken = semiToken.Substring(3, semiToken.Length - 6);

        return myAccessToken;
    }

Many thanks to http://codegist.net

Reference: http://codegist.net/code/spotify-c%23-example/