Hey Guys
its maybe a little bit off-topic, but I getting 404 Not found at https://api.twitch.tv/helix/channels and https://api.twitch.tv/helix/users. The weird thing is that I only get 404 as status code when I´m using Java but in Python basicly the same request works just fine
Here is my Java Code
public JSONObject getChannel(long id) {
HashMap<String, String> header = new HashMap<>();
header.put("Authorization", "Bearer " + TOKEN);
header.put("Client-Id", CLIENT_ID);
try {
return new JSONObject(sendRequest("GET", "https://api.twitch.tv/helix/channels?broadcaster_id=" + String.valueOf(id), header, new ArrayList<AbstractMap.SimpleEntry>()).getJSONArray("data").get(0));
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return null;
}
}
public JSONObject sendRequest(String method, String url, HashMap<String, String> header) throws IOException, InterruptedException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod(method);
for (String key : header.keySet()) {
connection.setRequestProperty(key, header.get(key));
}
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
if (connection.getResponseCode() != 200) {
System.out.println(connection.getResponseCode());
System.out.println(connection.getResponseMessage());
}
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null){
response.append(line);
response.append('\r');
}
rd.close();
return new JSONObject(response.toString());
}
And the Output is
404
Not Found
My Python code:
import requests
header = {
"Authorization" : "Bearer " + TOKEN,
"Client-Id" : CLIENT_ID
}
res = requests.get("https://api.twitch.tv/helix/channels?broadcaster_id=" + str(ID), headers=header)
print(res.json())
And the python code returns the correct data.
Where is my mistake in the Java Request?
Help is appreciated