-->
Page 1 of 1

Trouble making https requests to Microsoft servers

PostPosted: Sat Sep 01, 2018 4:00 am
by Nisse Hult
I have already posted this on the Arduino forums but didn't get much help so I thought I'd try here instead.

I have managed to set up an API with OAUTH2 on Azure. I want both my Arduino and Android application to be able to consume this API. To do this, they first need a Bearer token. I have been able to succesfully make a request with postman to get this token so that I then can make authorized requests with that token. Now I need to do the same request on my Arduino but it won't work. After a lot of trial and error I realized my code (probably) didn't work because microsoft servers uses HTTPS.

So to start off, this is the example I used: https://arduino-esp8266.readthedocs.io/ ... mples.html

Following that code, I can get it working! However, when I try to connect to the Microsoft servers instead, following the same proceedure, it fails. This is my code:


Code: Select allvoid setup() {
  Serial.begin(9600);
  connectWifi();
  RequestToken();
}

//Connects to a wifi network given username and password.
void connectWifi() {
  WiFi.begin(WIFI_SSID, WIFI_PWD);
  Serial.print("Connecting to wifi..");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected ");
  Serial.print(WiFi.localIP());
  Serial.println("");
}

void RequestToken(){
  const char* host = "login.microsoftonline.com";
  const char* fingerprint = "F3 B4 14 05 6D 8F B8 6D 98 FB 6F 28 2D 8F 45 1F 0A 87 BA 40";
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, 443)) {
  Serial.println("connection failed");
  return;
  }

  if (client.verify(fingerprint, host)) {
  Serial.println("certificate matches");
  } else {
  Serial.println("certificate doesn't match");
  }
}


This gives me the following output:

Code: Select allConnecting to wifi.........
WiFi connected 192.168.5.128
connecting to login.microsoftonline.com
connection failed


As I said, connecting to Github (using the example) works just fine! For reference, to actually get the token, here is Curl code for the postman request. (I have replaced the actual secrets with dummy data, but you get the idea) that I need to make:

Code: Select all--url https://login.microsoftonline.com/CLIENT_ID/oauth2/token \
      --header 'Cache-Control: no-cache' \
      --header 'Content-Type: application/x-www-form-urlencoded' \
      --header 'Postman-Token: XXX' \
      --header 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
      --form grant_type=client_credentials \
      --form client_id=YYY\
      --form 'client_secret=ZZZ' \
      --form 'resource=https://XYZ.azurewebsites.net