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

**URL:** <https://discuss.dev.twitch.com/t/trying-to-connect-an-iot-device-to-twitch-to-get-stream-status/29381>\
**Category:** API\
**Created:** [December 5, 2020, 8:24pm UTC](https://discuss.dev.twitch.com/t/trying-to-connect-an-iot-device-to-twitch-to-get-stream-status/29381 "2020-12-05T20:24:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeatherMartini](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/leathermartini/32/10821_2.png) [@LeatherMartini](https://discuss.dev.twitch.com/u/LeatherMartini)\
**Post date:** [December 5, 2020, 8:24pm UTC](https://discuss.dev.twitch.com/t/trying-to-connect-an-iot-device-to-twitch-to-get-stream-status/29381/1 "2020-12-05T20:24:21Z")

</div>

I’m trying to update this project from ADA Fruit - [https://learn.adafruit.com/3d-printed-iot-on-air-sign-for-twitch/software](https://learn.adafruit.com/3d-printed-iot-on-air-sign-for-twitch/software)  
I’ve got the ESP8266 connecting to [id.twitch.tv](http://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](https://id.twitch.tv/oauth2/token?client_id=%5BMyClientID%5D&client_secret=%5BMyClientSecret%5D&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](http://api.twitch.tv/helix/streams) to poll my channel. If there’s a better way to do this, I’m all ears.)

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [December 5, 2020, 8:43pm UTC](https://discuss.dev.twitch.com/t/trying-to-connect-an-iot-device-to-twitch-to-get-stream-status/29381/2 "2020-12-05T20:43:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![LeatherMartini](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/leathermartini/32/10821_2.png) [@LeatherMartini](https://discuss.dev.twitch.com/u/LeatherMartini)\
**Post date:** [December 5, 2020, 8:47pm UTC](https://discuss.dev.twitch.com/t/trying-to-connect-an-iot-device-to-twitch-to-get-stream-status/29381/3 "2020-12-05T20:47:03Z")

</div>

```
<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.)

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [December 5, 2020, 8:49pm UTC](https://discuss.dev.twitch.com/t/trying-to-connect-an-iot-device-to-twitch-to-get-stream-status/29381/4 "2020-12-05T20:49:23Z")

</div>

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

It should return JSON normally

![image](https://d2opxh93rbxzdn.cloudfront.net/original/2X/c/cd6cd1d3e14493fe32fe39ad00972852284795a4.png)

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

---

<div class="post-metadata">

**Author:** ![LeatherMartini](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/leathermartini/32/10821_2.png) [@LeatherMartini](https://discuss.dev.twitch.com/u/LeatherMartini)\
**Post date:** [December 5, 2020, 8:54pm UTC](https://discuss.dev.twitch.com/t/trying-to-connect-an-iot-device-to-twitch-to-get-stream-status/29381/5 "2020-12-05T20:54:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![LeatherMartini](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/leathermartini/32/10821_2.png) [@LeatherMartini](https://discuss.dev.twitch.com/u/LeatherMartini)\
**Post date:** [December 5, 2020, 9:51pm UTC](https://discuss.dev.twitch.com/t/trying-to-connect-an-iot-device-to-twitch-to-get-stream-status/29381/6 "2020-12-05T21:51:42Z")

</div>

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](https://id.twitch.tv/oauth2/token?client_id=MYID&client_secret=MYSECRET&grant_type=client_credentials) HTTP/1.1  
Host: [id.twitch.tv](http://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
> 
> ```

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [December 5, 2020, 9:55pm UTC](https://discuss.dev.twitch.com/t/trying-to-connect-an-iot-device-to-twitch-to-get-stream-status/29381/7 "2020-12-05T21:55:44Z")

</div>

![image](https://d2opxh93rbxzdn.cloudfront.net/original/2X/1/1b79460c9d3593cd2a6f635d2767486a8c4cc16c.png)

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

---

<div class="post-metadata">

**Author:** ![LeatherMartini](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/leathermartini/32/10821_2.png) [@LeatherMartini](https://discuss.dev.twitch.com/u/LeatherMartini)\
**Post date:** [December 5, 2020, 11:04pm UTC](https://discuss.dev.twitch.com/t/trying-to-connect-an-iot-device-to-twitch-to-get-stream-status/29381/8 "2020-12-05T23:04:09Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![LeatherMartini](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/leathermartini/32/10821_2.png) [@LeatherMartini](https://discuss.dev.twitch.com/u/LeatherMartini)\
**Post date:** [December 6, 2020, 3:29am UTC](https://discuss.dev.twitch.com/t/trying-to-connect-an-iot-device-to-twitch-to-get-stream-status/29381/9 "2020-12-06T03:29:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [January 5, 2021, 3:29am UTC](https://discuss.dev.twitch.com/t/trying-to-connect-an-iot-device-to-twitch-to-get-stream-status/29381/10 "2021-01-05T03:29:06Z")

</div>

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