2

I am trying to use Beyond Verbal RST API to post voice sample data over HTTP post method from ESP8266. The first step for the API communication is to get access token using the POST method. You can check the following codes. With this code I am just getting "failed to Post" response on serial output.

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
const char *ssid = "xxxxxx";
const char *pass = "xxxxxx";
String token;

HTTPClient https;
WiFiClientSecure client;
String getRecordID(String stoken);

void setup() {

Serial.begin(115200);
Serial.println("connecting to network..");
WiFi.begin(ssid, pass);

while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
}
Serial.println("conntected to network..");
}



void loop() {

String ret;
token = getAccessToken();
delay(2000);
Serial.println(token);
}




String getAccessToken(){
//    client.setInsecure();
  const char * host = "token.beyondverbal.com";
  const uint16_t port = 443;
  const char * path = "/token";

  StaticJsonBuffer<1000> jb;
  String res;
  Serial.println("conntecting to server..");

  if (https.begin(client, host, port, path)) {
    https.addHeader("Content-Type", "x-www-formurlencoded");
    int httpsCode = https.POST("grant_type=client_credentials&apiKey=1d0956a4-3deb-431a-b3e0-45f5c371fe99");
        if (httpsCode > 0) {
              if (httpsCode == HTTP_CODE_OK) {
                JsonObject& obj = jb.parseObject(https.getString());
                String token = obj["access_token"];
                if (obj.success()) {
                   res =  token;
                } else {
                    res = "failed to parse json";
                }                
              }
        } else {
            res = "failed to Post";
        }
   } else {
    res = "failed to connect to server";
   } 
https.end();
return res;
}

Check out the guideline documentation and please read the authentication part. I have followed the steps and tried in several ways, but still no luck.

But my API code and others parameter are ok. I have tried API post method from Mozilla Firefox addon and different platform. From everywhere I got the token successfully. But I am still unable to get the token with my code.

Please check and me a solution regarding the issue.

1 Answer 1

0

Use these libraries. ESPAsyncTCP, asyncHTTPrequest

then use below code. Code for sample.

#include <ESPAsyncTCP.h>
#include <asyncHTTPrequest.h>

asyncHTTPrequest client;
asyncHTTPrequest client2;

void onClientStateChange(void * arguments, asyncHTTPrequest * aReq, int readyState) {
  Serial.println(readyState);
  switch (readyState) {
    case 0:
      // readyStateUnsent     // Client created, open not yet called
      break;

    case 1:
      // readyStateOpened     // open() has been called, connected
      break;

    case 2:
      // readyStateHdrsRecvd  // send() called, response headers available
      break;

    case 3:
      // readyStateLoading    // receiving, partial data available
      break;

    case 4:
      // readyStateDone       // Request complete, all data available.
#ifdef SERIAL_DEBUG
      Serial.println(aReq->responseHTTPcode());
#endif
      if (aReq->responseHTTPcode() != 200) {
#ifdef SERIAL_DEBUG
        Serial.println("return");
#endif
        return;
      }
      String response = aReq->responseText();
#ifdef SERIAL_DEBUG
      Serial.println(response.c_str());
#endif
      break;
  }
}

void setupClient() {
  String URL = "dummy.restapiexample.com/api/v1/create";

  client.setTimeout(5);
  client.setDebug(false);
  client.onReadyStateChange(onClientStateChange);

  client.open("POST", URL.c_str());
  client.setReqHeader("Content-Type", "application/json");
  client.send("{\"name\":\"test\",\"salary\":\"123\",\"age\":\"23\"}");

  String URL2 = "jsonplaceholder.typicode.com/users";

  client2.setTimeout(5);
  client2.setDebug(false);
  client2.onReadyStateChange(onClientStateChange);

  client2.open("GET", URL2.c_str());
  client2.send();
}

Always connect with async client as it will not block your main execution until you will get a response.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.