Skip to content

Instantly share code, notes, and snippets.

@rmxsantiago
Last active November 15, 2022 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmxsantiago/c1611aaf4250140c13b13591d7629530 to your computer and use it in GitHub Desktop.
Save rmxsantiago/c1611aaf4250140c13b13591d7629530 to your computer and use it in GitHub Desktop.
Twich API
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <Arduino_JSON.h>
#include <sstream>
#include <string>
#include <cstring>
#ifndef STASSID
#define STASSID "TUA REDE WIFI"
#define STAPSK "TEU PASSWORD DO WIFI"
#endif
const String clientId = "TEU CLIENT ID";
const String clientSecret = "TEU CLIENT SECRET";
const char* ssid = STASSID;
const char* password = STAPSK;
const char* host = "id.twitch.tv";
const uint16_t port = 443;
String access_token = "";
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}
void loop() {
//digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
//delay(1000); // wait for a second
//digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
//delay(1000); // wait for a second
Serial.print("connecting to ");
Serial.print(host);
Serial.print(':');
Serial.println(port);
// Use WiFiClient class to create TCP connections
WiFiClientSecure client;
client.setInsecure();
if (!client.connect(host, port)) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);
return;
}
String data = "client_id=" + clientId + "&client_secret=" + clientSecret + "&grant_type=client_credentials";
// This will send the request to the server
client.println("POST /oauth2/token HTTP/1.1");
client.println("Host: id.twitch.tv");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: " + String(data.length()));
client.println();
client.println(data);
//read back one line from server
Serial.println("receiving from remote server");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
delay(60000);
return;
}
}
std::stringstream ss;
bool capturing = false;
while (client.available()) {
char ch = static_cast<char>(client.read());
if(ch == '{' || capturing == true || ch == '}') {
capturing = true;
ss << ch;
if(ch == '}') {
capturing = false;
}
}
}
JSONVar json = JSON.parse(ss.str().c_str());
access_token = String((const char*) json["access_token"]);
Serial.println(access_token);
if (!client.connect("api.twitch.tv", port)) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);
return;
}
Serial.println("GET /helix/streams?user_login=julialabs HTTP/1.1");
// This will send the request to the server
client.println("GET /helix/streams?user_login=julialabs HTTP/1.1");
client.println("Host: api.twitch.tv");
client.println("Authorization: Bearer " + access_token);
client.println("Client-Id: " + clientId);
client.println();
client.println();
timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 10000) {
Serial.println(">>> Client Timeout !");
client.stop();
delay(60000);
return;
}
}
ss.clear();
delay(1000);
capturing = false;
String response = "";
while (client.available()) {
char ch = static_cast<char>(client.read());
if(ch == '{' || capturing == true || ch == '}') {
capturing = true;
response.concat(ch);
if(ch == '}') {
capturing = false;
}
}
}
Serial.println("Recebendo stream data");
Serial.println(response);
Serial.println("closing connection");
client.stop();
Serial.println("wait 5 sec...");
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment