Trying to connect an IoT device to Twitch to get stream status

I’m trying to update this project from ADA Fruit -
I’ve got the ESP8266 connecting to successfully, but when I send it:

I get back a 400 Bad Request error.

What am I missing?
(I’m trying to do this to get the OAuth token and then use that to get to to poll my channel. If there’s a better way to do this, I’m all ears.)

What’s the body of the error message say?

It should tell you what the issue is

HTTP 400 could be one of many things. You only have half the picture here.

<head><title>400 Bad Request</title></head>
<center><h1>400 Bad Request</h1></center>

That’s what I get back in the serial monitor on the chip. Here’s the code to get that:
while (client.connected()) {
String line = client.readStringUntil(’\n’);
if (line == “\r”) {
Serial.println(“headers received”);
} else {
Serial.println(String("Headers: ") + line);

Serial.println(“reply was:”);
String line;
line = client.readStringUntil(’\n’); //Read Line by Line
Serial.println(line); //Print response
Serial.println(“closing connection”);

Those all show up with the “Headers” tag in the Serial monitor, so they’re being processed in the inital “headers” section there. From what I can tell, I’m not getting any other information back from the server. (Or I’m missing the code needed to catch it.)

You have something really bad going wrong as it shouldn’t return HTML

It should return JSON normally


So that may suggest you have outbound headers doing something weird. Or you are not sending the request you think you are sending?

Got it. So here’s my POST command in the code:
client.print(String(“POST “) + OAUTHURL + “?client_id=” + CLIENTID + “&client_secret=” + CLIENTSECRET +”&grant_type=client_credentials\r\n” +
“Content-Type: application/json” +
“Accept: application/json”);

Have I messed something up there? Or missed something? Or should the string I send the https connection read more:
curl -X POST …
with some -H lines as well?

So adding curl -X into the code, so the full line being sent to the server is:
curl POST HTTP/1.1
Content-Type: application/json
Accept: application/json
Connection: close

Gets the response:

    Headers: HTTP/1.1 400 Bad Request
    Headers: Server: awselb/2.0
    Headers: Date: Sat, 05 Dec 2020 21:47:37 GMT
    Headers: Content-Type: text/html
    Headers: Content-Length: 122
    Headers: Connection: close
    headers received
    reply was:
         <head><title>400 Bad Request</title></head>
         <center><h1>400 Bad Request</h1></center>

    closing connection

if I run

curl -X POST ""

my result is.

And if I used real creds, I get a token JSON blob

So I have no idea why you are getting HTML and a 400

As your request looks correct

Changed the methods I was using to POST to the server and I’m getting helpful error messages now. (Down to “missing client secret” which I’m sure is just missing the formatting I need to get it to recognize it.)

Got it! Had to strip the headers out and just pass the params in the POST. Got it working!

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