HTTP request to Google Calendar API fails


i have been trying to get to the events of my google calendar via a http POST request for quite some time now. I want to build a room occupancy display using ESP32. Therefore a calendar is created, in which an appointment is entered. The display at the room should show from when until when the room is free or occupied. This should then be extended to several rooms.

Google describes it as follows. First you have to send a POST request to get access to the API. With the help of the received code you send a new POST request to get the access token. With this token and a few other parameters you get the information from the calendar.

With Matlab I have already successfully read my calendars. That means that this should work so far.

But the implementation in ESP32 fails already at the first POST request. According to google it should look like this:

POST /o/oauth2/device/code HTTP/1.1
Content-Type: application/x-www-form-urlencoded


I've tried two variations. First using HTTPclient from the ESP32 library:

void getGoogleAPIaccessHTTP()
  Serial.println("start HTTP function");
  HTTPClient http;

  String host = "";
  String url = "/o/oauth2/device/code";

  String postCode = ("client_id=" + ClientID + "&scope=" + CalenderScope);
  http.begin(host + url);
  http.addHeader("Host", "", "Content-Type", "application/x-www-form-urlencoded");
  Serial.println("Request URI");

  int httpResponseCode = http.POST(postCode);

  if (httpResponseCode > 0)
    String payload = http.getString();
    String response = http.getString(); //Get the response to the request
    Serial.println(response); //Print request answer

  http.end(); //Free resources

in answer I receive:

  "Error: {
    "Code": 400,
    "message": "Invalid JSON payload received. Unexpected token.\nclient_id=1234567890\n^",
    "status": "INVALID_ARGUMENT"

where exactly 10 digits of the ClientID are returned. However, this is 72 digits large.

My second attempt was to use the WiFiClientSecure (here I anticipated, also the WiFiClient did not lead to success):

void getGoogleAPIaccessWS()
  Serial.println("start WiFi-http function");
  WiFiClientSecure wsclient;
  String postString = (String("POST ") + "/o/oauth2/device/code" + " HTTP/1.1\r\n" +
             "Host:\r\n" +
             "Content-Type: application/x-www-form-urlencoded\r\n\r\n "+
             "client_id=" + ClientID + "&\r\n" + "scope=" + CalenderScope +"\r\n");

  if (wsclient.connect("",443));
  //if (wsclient.connect("",443));
  //if (wsclient.connect("",443));
  //if (wsclient.connect("",443));
    Serial.println("Request URI");

  if (wsclient.available())
    Serial.println("Data available");

    int line = wsclient.readStringUntil('\r');
    Serial.println("received string:");

  wsclient.stop(); //Free resources


Result here, wsclient.available() will never be true.

Can anyone here give me the decisive tip or hint as to what I'm doing wrong?

Thank you very much.

Take a look at the WiFiClientSecure exmple. You have to specify a CA certificate in order to connect to a HTTPS server. The server parameter of the connect() method is a hostname, not a URL or any wild part of a URL.