Java - v5 - How to get the current community for a channel

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

Your API logic is correct but make sure twitchName is a Twitch channel ID (ex: 12826) and not the channel name. The comment seems to indicate you are trying to pass a channel name and v5 requires channels IDs.

/channels/:channel_id/community is an unauthenticated request so you don’t need to worry about scopes, just make sure you are passing a valid client ID (not OAuth token) in the header.

Thank you, I was going mad wondering what I was doing wrong. I swapped the name pull to get the “_id” member and it worked perfectly.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.