Hello,
I am a new Java developer working on a dashboard application. One of the things I would like to do is pull the current community from a channel to display with the application. I was able to do this with the channel for status and game, but I am stumbling with communities.
I read the API documentation on get channel community, but I am having issues translating the curl into an HttpURLConnection request. Below is my current code, can someone point me to what I may be doing wrong? I get a code 400 when attempting to call this method.
I am unsure, but I think the issue is with the json parse try block. For reference I am using the gson library for this. Additionally I have “Channel_Editor” in my scopes.
public String getTwitchCommunity() { String output = null; URL requestUrl = null; HttpURLConnection connection = null; JsonParser jp = new JsonParser(); try { requestUrl = new URL("https://api.twitch.tv/kraken/channels/"+ twitchName +"/community"); // twitchName is a String set at authorization EX "figbird" when I auth } catch (MalformedURLException e) { e.printStackTrace(); } try { assert requestUrl != null; connection = (HttpURLConnection) requestUrl.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/vnd.twitchtv.v5+json"); connection.setRequestProperty("Client-ID", clientID); // a stored value of my clientID connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); } catch (IOException e) { e.printStackTrace(); } try { assert connection != null; JsonElement root = jp.parse(new InputStreamReader((InputStream) connection.getContent())); JsonObject rootobj = root.getAsJsonObject(); output = rootobj.get("name").getAsString(); } catch (IOException e) { e.printStackTrace(); } return output; }
Cheers,
FIGBird