Java Based webhook usage (if possible)


#1

So, what I’m trying to do is use a java CLI program running both a twitch and discord bot to connect to the webhook system to auto ping on stream status changing, but I seem to be running into an issue.

Is this something that’s even possible using java? Am I just trying to call the initial …/…/hub url wrong? Any assistance would be appreciated.

String payload = "{" +
            "\"hub.callback\": \"http://IP.of.my.raspberrypi:4568\", " +
            "\"hub.mode\": \"subscribe\", " +
            "\"hub.lease_seconds\": \"50000\", " +
            "\"hub.topic\": \"https://api.twitch.tv/helix/users/follows?first=1&from_id=64207063\"" +
            "}";

    StringEntity entity = new StringEntity(payload,
            ContentType.APPLICATION_FORM_URLENCODED);

    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost("https://api.twitch.tv/helix/webhooks/hub");
    request.setEntity(entity);
    request.setHeader("Accept", "application/json");
    request.setHeader("Client-ID", "IDHERE" );
    request.setHeader("Authorization", "OAuth OAUTHHERE" );
    request.setHeader("Content-Type", "application/json; utf-8" );


    HttpResponse response = httpClient.execute(request);
    System.out.println(response.getStatusLine().getStatusCode());

That’s the rough base for calling the initial URL. The URL points to the IP of my raspberry pi, which is running a java web server which SHOULD, from my understanding, be able to handle the response just fine, however trying to test the code above gives me a 400 error and I’m not entirely sure why.


#2

Or, conversely, should I refactor my bots to use something like the twitch4j wrapper instead of what I’m currently using (I believe pircbotX, which is no longer really maintained, resulting in me having to try and do things like this by hand lol)


#3

Check the body of the response, you should have an error message that will help explain what the issue is.

Also, I don’t think this will be the cause of your 400 error, but for all Helix endpoints the Authorization prefix is Bearer, not OAuth.


#4
400 Bad Request [access-control-allow-origin: *, cache-control: no-cache, no-store, must-revalidate, private, content-type: application/json; charset=utf-8, expires: 0, 
pragma: no-cache, server: envoy, timing-allow-origin: https://www.twitch.tv, twitch-trace-id: 6d2f56fabfc54aaf2cd34e4f72706d35, x-ctxlog-logid: 1-5de46ac4-6c1af8c9faf2a349f5e7f87a, Accept-Ranges: bytes, Via: 1.1 varnish, Content-Length: 79, Accept-Ranges: bytes, Date: Mon, 02 Dec 2019 01:37:08 GMT, Via: 1.1 varnish, 
Connection: keep-alive, X-Served-By: cache-sea4449-SEA, cache-mdw17380-MDW, X-Cache: MISS, MISS, X-Cache-Hits: 0, 0, X-Timer: S1575250629.909181,VS0,VS0,VE61, Vary: Accept-Encoding, Strict-Transport-Security: max-age=300] ResponseEntityProxy{[Content-Type: application/json; charset=utf-8,Content-Length: 79,Chunked: false]}}

That’s the full body, I have honestly no idea what It’s saying is wrong


#5

My quick guess: hub.lease_seconds should be an int instead of a string

So replace:

"\"hub.lease_seconds\": \"50000\", " +

With:

"\"hub.lease_seconds\": 50000, " +

#6

Nope, tweaked it to remove those string quotes and it still gives me a 400 error.


#7

This worked fine for me:

$ curl -o /dev/null -s -w "%{http_code}\n" \
  -H 'Client-ID: my_client_id_here' -H "Content-Type: application/json" \
  -d '{"hub.callback": "http://IP.of.my.raspberrypi:4568", "hub.mode": "subscribe", "hum.lease_seconds": 50000, "hub.topic": "https://api.twitch.tv/helix/users/follows?first=1&from_id=64207063"}' \
  -X POST https://api.twitch.tv/helix/webhooks/hub
202

Unfortunately I’m not familiar enough with Java to help debug. But given the CURL version works perfectly fine, I’d be suspicious of how the request payload is being created in Java.


#8

I appreciate it, that at least let’s me know it’s none of my auth codes. I’m probably going to look into a better library for creating and parsing HTTP calls to make this hopefully work. I know people have working websocket connections in other code, so I know it should be possible, I’m just not sure where MINE is breaking.