Hi All -- I am not sure where I am doing this wrong... I am trying to retrieve the feedback from Google API with the HTTPClient but it gets stuck on the getString() call.
However, if I try a different link, it works.
HTTPClient http;
//String request = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=" + sCommand; // this link does not work
String request = "https://www.howsmyssl.com/a/check"; // this link works
Serial.println(request);
http.begin(request); //Specify the URL
int httpCode = http.GET(); //Make the request
if (httpCode > 0) { //Check for the returning code
String payload = http.getString(); // <<<<< here it is where it gets stuck when I try the google api link above...
Serial.println(httpCode);
Serial.println(payload);
}
else {
Serial.println("Error on HTTP request");
}
http.end(); //Free the resources
and the entire code, if you wish to revise everything
// Load library
#include <WiFi.h>
#include <HTTPClient.h>
// Replace with your network credentials
const char* ssid = "mywifinetwork";
const char* password = "mypasswd";
// Set web server port number to 80
WiFiServer server(80);
WiFiClient client;
char buffer[2048]; // buffer for debugging
/*----------------------------------------------------------------------------*/
/* incoming data : Get a single client char */
/*----------------------------------------------------------------------------*/
char gchr(void) {
while (!client.available()); /* Await data from client */
return client.read(); /* Return input character */
} /* end: gchr() */
/*----------------------------------------------------------------------------*/
/* incoming data : Get an entire line from the client */
/*----------------------------------------------------------------------------*/
char *glin(char *buf) {
char c, *p = buf; /* Input char, input buffer pointer */
while (' ' > (c = gchr())); /* Discard (leading) control chars */
do *p++ = c; /* Move input char to line buffer */
while (' ' <= (c = gchr())); /* Until control char encountered */
*p = '\0'; /* Terminate line in buffer */
return buf; /* Return pointer to input string */
} /* end: glin() */
// Auxiliar variables to store the current output state
String output_led_State = "off";
// Assign output variables to GPIO pins
const int output_led = 2;
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output_led, OUTPUT);
// Set outputs to LOW
digitalWrite(output_led, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(void) {
String sCommand;
if (client = server.available()) { // Request client connection
Serial.println("New Client."); // print a message out in the serial port
while (client.connected()) { // Is there client data available?
glin(buffer); // Get HTTP request line by line
sCommand = String(buffer);
sCommand.replace("GET /", "" );
sCommand.replace(" HTTP/1.1", "" );
Serial.print("Received: "); // Show what we received from
Serial.println(sCommand); // the current client
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<head>");
client.println("<meta name=\"google-signin-scope\" content=\"profile email\">");
client.println("<meta name=\"google-signin-client_id\" content=\"407552142020-pkaibjj992iuootk5s7isjpuhj1sfgjk.apps.googleusercontent.com\">");
client.println("<script src=\"https://apis.google.com/js/platform.js\" async defer></script>");
client.println("</head>");
client.println("<body>");
// tests if received proper user
if (sCommand != "") {
Serial.println(sCommand);
client.println(sCommand);
HTTPClient http;
//String request = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=" + sCommand; // this link does not work
String request = "https://www.howsmyssl.com/a/check"; // this link works
Serial.println(request);
http.begin(request); //Specify the URL
int httpCode = http.GET(); //Make the request
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
}
else {
Serial.println("Error on HTTP request");
}
http.end(); //Free the resources
} else {
client.println("<div class=\"g-signin2\" data-onsuccess=\"onSignIn\" data-onfailure=\"onSignInFailure\" data-theme=\"dark\"></div>");
client.println("<script>");
client.println("function onSignIn(googleUser) {");
client.println("var currentState = history.state;");
client.println("window.location = \"http://coxinha.asuscomm.com:9020/\" + googleUser.getAuthResponse().id_token;");
client.println("history.replaceState(currentState , \"\", \"http://coxinha.asuscomm.com:9020/\");");
client.println("}");
client.println("</script>");
}
client.println("</body>");
client.println("</html>");
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
}