Search code examples
androidauthorizationspotify

Spotify Android - Make Authorized Web Requests


I'm working on an Android app that implements the Spotify API to allow the users to listen to music. I've configured the Player that Spotify has created for android devices, but it's incredibly limited in terms of its functionality, so I've had to go through Spotify's Web API to do more advanced features.

I've hit a bug when trying to get a list of the user's own playlists. I'm making a request using:

URL url = new URL("https://api.spotify.com/v1/me/playlists");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

But instead of this command going through like it does for the other web API requests I've made, it throws the error:

java.io.FileNotFoundException: https://api.spotify.com/v1/me/playlists                                                                               
   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242)                                                                              
   at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
   at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
   at com.tmacstudios.spotifyvoice.WebWrapper$override.searchUserPlaylist(WebWrapper.java:257)
   at com.tmacstudios.spotifyvoice.WebWrapper$override.access$dispatch(WebWrapper.java)
   at com.tmacstudios.spotifyvoice.WebWrapper.searchUserPlaylist(WebWrapper.java:0)
   at com.tmacstudios.spotifyvoice.MainActivity$6.run(MainActivity.java:382)
   at java.lang.Thread.run(Thread.java:818)

I'm not entirely sure why I'm getting this error, but I think it may have to do with not having the necessary authorization to make this request. Can anyone please help me solve this problem?


Solution

  • After studying the documentation some more, I was able to figure out a way to solve my problem. I was in fact getting the error since I wasn't using the proper authorization.

    You can get the authorization token in OnActivityResult using this code:

    AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, intent);
                if (response.getType() == AuthenticationResponse.Type.TOKEN) {
    
                    authToken = response.getAccessToken();
                    Log.e("MainActivity","Auth Token: "+authToken.toString());
     ...
    

    Then when making the URL request, just pass in the authorization token as a header.

    URL url = new URL("https://api.spotify.com/v1/me/playlists");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestProperty("Authorization", "Bearer " + authToken);
    
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
     ...
    

    Also note that you must have the permissions you are using enabled as scopes when you are forming the authorization request initially.