I'm trying to integrate the Genius API into my Java program but I'm not entirely sure what I'm doing with making the actual HTTP request. Here's the code I'm trying to use:
URLConnection connection = new URL("https://api.genius.com/search?q=juice").openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Bearer TOKEN");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
return content.toString();
(where TOKEN is my Genius auth token)
but I'm getting an IOException with a 403 forbidden message.
java.io.IOException: Server returned HTTP response code: 403 for URL: https://api.genius.com/search?q=juice
This same request worked when I tried with Hurl.it:
so I'm not exactly sure what's going on here. When I tried printing out the headers to see if they went through, I got this:
System.out.println("auth: " + connection.getHeaderField("Authorization"));
System.out.println("content: " + connection.getHeaderField("Content-Type"));
/** output */
auth: null
content: text/html; charset=UTF-8
I would appreciate any help here - thank you!!