0
votes

I want to make some searches in spotify database only for learning purpose of rest app. I'm stuck on the first point of https://developer.spotify.com/documentation/general/guides/authorization-guide/ Authorization Code Flow to login in spotify with java app:

package spotify;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/*
    1) https://developer.spotify.com/dashboard/applications
*/

public class SpotifyMain {

    static final String CLIENTID = "blablablablablablablabla";
    static final String CLIENTSECRET = "blablablablablablablabla";
    static final String REDIRECTURL = "https://github.com/blablablablablablablabla"; //whiltelisted set inside spotify

    public static void main(String[] args) {
        try {
            String url_auth = 
            "https://accounts.spotify.com/authorize?"
            + "client_id="+CLIENTID+"&"
            + "response_type=code&"
            + "redirect_uri="+REDIRECTURL+"&"
            + "scope=user-read-private%20user-read-email&"
            + "state=34fFs29kd09";

            System.out.println(url_auth);

            URL url = new URL(url_auth);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }
            conn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

I get error 406. The url I think is correct because if I put that url inside a browser it displays correctly that the app want to access some resources. If I put that url inside postman the error changes in 400 bad request... Am I missing something? thanks

1

1 Answers

0
votes

Try removing this line:

conn.setRequestProperty("Accept", "application/json");

HTTP 406 Error is "Not Acceptable" meaning the server has generated a reply that it can't return as the requested Accept content type.