HTTP request to Google Calendar API fails

Hello,

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
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

client_id=client_id&
scope=APIscope

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

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

  String host = "https://accounts.google.com";
  String url = "/o/oauth2/device/code";

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

  int httpResponseCode = http.POST(postCode);

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

    delay(2000);
    
  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: accounts.google.com\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("accounts.google.com/o/oauth2/device/code",443));
  //if (wsclient.connect("https://accounts.google.com/o/oauth2/device/code",443));
  //if (wsclient.connect("accounts.google.com",443));
  //if (wsclient.connect("https://accounts.google.com",443));
    {
    Serial.println("connected");
    Serial.println("Request URI");
    Serial.println(postString);
    wsclient.print(postString);
    
    delay(1000);
    }

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

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

  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.