Making Request to new twitch API in java

Hi, so I’ll start off with I have no idea what I’m doing, I am trying to do a lot of things. I have no experience with server requests but I am trying to make a discord bot that will monitor a group of people to see when they are online on twitch and post them to a server. So the part that I am not understanding at all is how to make a request form the twitch api in order to get the JSON of there status. I am currently looking at this link https://dev.twitch.tv/docs/v5/reference/streams/#get-stream-by-user, and it shows this snippet

curl -H 'Accept: application/vnd.twitchtv.v5+json' \
-H 'Client-ID: uo6dggojyb8d6soh92zknwmi5ej1q2' \
-X GET 'https://api.twitch.tv/kraken/streams/44322889'

So from my understanding I need to submit a request to the api following that forat with replacing the userid with the channel I would like to check. However, I have no idea how to do that. I copied the following java code

public static String executePost(String channelName){
    URL url;
    HttpURLConnection connection = null;

    try{
        //create connection
        url = new URL(("https://api.twitch.tv/kraken/streams?channel="  + channelName));
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");

        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream(
                connection.getOutputStream());

        //Get response
        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 response.toString();
    } catch (Exception e){
        e.printStackTrace();
        return null;
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

So currently, I am trying to make an http request from java in order to get the JSON, but I have also read you that you need an authentication in the request, which I have no idea how I would do that. I set up my twitch application and have access to the client ID and secret ID, I just don’t know what to do with them. If anyone can give me an example or point me to sources to help learn more about this, I would be greatly appreciative!

For the API method you are calling, to put it simply, No authentication is required. Aside from a header containing a Client ID.

You are calling

Which is a GET request not a POST like in your snippet.
This should do what you need:

    url = new URL(("https://api.twitch.tv/kraken/streams?channel="  + channelName));
    connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Client-ID",
            YOURCLIENTIDHERE);

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