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

I’m trying to update this project from ADA Fruit - https://learn.adafruit.com/3d-printed-iot-on-air-sign-for-twitch/software
I’ve got the ESP8266 connecting to id.twitch.tv successfully, but when I send it:
POST https://id.twitch.tv/oauth2/token?client_id=[MyClientID]&client_secret=[MyClientSecret]&grant_type=client_credentials

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 api.twitch.tv/helix/streams 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.

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

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”);
break;
} else {
Serial.println(String("Headers: ") + line);
}
}

Serial.println(“reply was:”);
Serial.println("==========");
String line;
while(client.available()){
line = client.readStringUntil(’\n’); //Read Line by Line
Serial.println(line); //Print response
}
Serial.println("==========");
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

image

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 https://id.twitch.tv/oauth2/token?client_id=MYID&client_secret=MYSECRET&grant_type=client_credentials HTTP/1.1
Host: id.twitch.tv
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:
    ==========
         <html>
         <head><title>400 Bad Request</title></head>
         <body>
         <center><h1>400 Bad Request</h1></center>
         </body>
        </html>


    ==========
    closing connection

if I run

curl -X POST "https://id.twitch.tv/oauth2/token?client_id=MYID&client_secret=MYSECRET&grant_type=client_credentials"

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.