Twitch ID client of Application

I’m trying to use the id client of my application in my java project, but when i try to use it he say:
Answer Code of Twitch API : 401

Did you generate/obtain a Token to use.

Twitch API requires both a Client ID and a Token (excluding schedule and video ingest)

The Body of the error response should also describe this error, the HTTP Code is half the information

This is my code in java, and when i call it he return 401, i need something more?

private boolean checkTwitchLiveStatus() {
        String clientId = "the_client_id_from_the_app_manager";
        String channelName = "my_channel_name";

        try {
            URL url = new URL("https://api.twitch.tv/helix/streams?user_login=" + channelName);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Client-ID", clientId);
            conn.setRequestProperty("Accept", "application/vnd.twitchtv.v5+json");

            int responseCode = conn.getResponseCode();
            System.out.println("Codice di risposta dell'API di Twitch: " + responseCode); //responseCode is the code 401

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();

                String jsonResponse = response.toString();
                System.out.println("Risposta JSON dall'API di Twitch: " + jsonResponse);

                return jsonResponse.contains("\"type\":\"live\"");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false; // Default: live non attiva
    }

conn.setRequestProperty(“Accept”, “application/vnd.twitchtv.v5+json”);

the v5 header is for v5 kraken, which is loooong gone.
This header is not needed

Theres no Authorization token present in your request

First off, conn.setRequestProperty("Accept", "application/vnd.twitchtv.v5+json"); is not needed, that’s a header for v5, which isn’t a thing any more.

Secondly, you’re not sending an OAuth token. If you follow the Getting Stared guide it’ll explain how to get an OAuth token.

on the second line of the code, u can see String clientId, here i have my Id taked from Twitch Developers

You require an oAuth token to make a request as outlined in the Getting Started guide we linked.

You are not generating and using the required token.

maybe i understand a little bit more than 10 hours ago,
actually i need to learn how donwload and use cURL
and i learned how use my actually Client Id and Secret Id (maybe)
and nothing, i will return here if i need help :3 Thanks Guys <3 (sorry for my english)

i actually created the OAuth token using the guide, i used my virtual machine for create it, after, i used this code for see if the code work or not. (spoiler, don’t work…)

private static boolean validateOAuthToken() {
        try {
            OkHttpClient client = new OkHttpClient();

            // Crea una richiesta API che richieda l'autorizzazione del token
            Request request = new Request.Builder()
                    .url("https://api.twitch.tv/helix/users")
                    .header("Client-ID", "YOUR_TWITCH_CLIENT_ID") //i tried with my name and after with // ID client
                    .header("Authorization", "Bearer " + OAUTH_TOKEN)
                    .build();

            Response response = client.newCall(request).execute();

            int statusCode = response.code();
            return statusCode == 200;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }

EVERY time error 401…

What is the body message of the response, it’ll describe the error.

If you generated a client credentials token that will not return a user as you didn’t ask for a user and there is no user in the token.

how i can check if my OAuth token is valid? because every thing i use just return code 401…

You need to extract the body of the response, as this will describe the issue.

The HTTP Code alone will not help you.

You can use the validate api to validate a token, but if you just generated a token it will should be valid.

okay, i tried and he saw cliend_id: “my id” and scopes: “null” and expires_in : time.

now i’m trying to learn how use the OAuth token on my java project for get back my twitch channel status (if in live or not in live)

actually i use this:

curl -X GET 'https://api.twitch.tv/helix/streams?user_login=my_twitch_name' \
-H 'Authorization: Bearer OAUTH_TOKEN' \
-H 'Client-Id: MY_CLIENT_ID'

but every call i make he return me a JSON with 15 or more things, and this is okay, but i need to check only if i’m in live, and IF i’m in live, i will take this JSON for title, name of game and something more.

actually i tried with

curl -X GET 'https://api.twitch.tv/helix/streams/key'

but he give me error 400… and if i add the broadcaster_id he give me error 401 :frowning:
any idea about it? i can’t find nothing…

Working as expected here, calling https://api.twitch.tv/helix/streams?user_login=cohhcarnage

I get a data array of one items (or none if cohhcarange wasn’t live)

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