Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Nate1661
#78213 Hi, I have a sketch that requests a device token from Facebook in order to authenticate a Wemos D1 Mini.

All in all, I want to reproduce the following, which gives me a device code in less than a second:
Code: Select allcurl https://graph.facebook.com/v2.7/device/login -d "type=device_code&access_token=MYTOKEN"


I set up the following sketch which works, albeit very slowly.
It fetches the device token in about 17 seconds.
It seems that String response = client.readString(); is the culprit.

Can any offer advice on why that could be, and how to possibly remedy it?

Thank you kindly,

Nate
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>

void setup() {
    /* code to connect to WiFi */
    Serial.println(getFBDeviceToken());
}

String getFBDeviceToken(){

  //Connect to FB SSL
  if(!client.connect(host, httpPort)){
    return "** Failed to connect **";
  }
 
  client.println("POST " + path + " HTTP/1.1");
  client.println("Host: " + String(host));
  client.println("User-Agent: ESP8266/1.0");
  client.println("Connection: close");
  client.println("Content-Type: application/x-www-form-urlencoded;");
  client.print("Content-Length: ");
  client.println(data.length());
  client.println();
  client.println(data);
 
  String response = client.readString();
  int bodypos =  response.indexOf("\r\n\r\n") + 4;
 
 
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(response.substring(bodypos));
 
  String device_token = root[String("user_code")];

  return device_token;
}