-->
Page 1 of 2

Https problem with nest_api in http library?

PostPosted: Sat Feb 10, 2018 11:55 am
by pingwing
Hi Guys,

I'm trying to connect with the Nest_api (https://developers.nest.com/)
Beside the difficult security issues I'm encountering, I'm also unable to even get data from the API :cry:
I've tried a couple of things and I always get an 'redirection' error. Meaning that I've no acces.
I'm starting to believe that I've some problems with the HTTPS requests. Did somebody had similar issues?

You can check my code here:
The part where I try to access the server is the last piece of code.

Code: Select allBasic Infos

The goal is to let a switch respond on the data received from my nest Thermostad. I'm using the Nest_API to get the result
Hardware

Hardware: ESP8266
Description

I'm trying to access the Nest API without any success. I always get some kind of redirection error.
Or I get the 307 error code (redirection) or an 400. With the first one, there's no redirection location.

What do i do wrong? Is it possible that the library doesn't support HTPPS? Is there a solution for it?
Settings in IDE

Module: Generic ESP8266 Module
Sketch

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>

#define USE_SERIAL Serial

String accessurl = "https://api.home.nest.com/oauth2/access_token";
MDNSResponder mdns;

// My Network cred.
const char* ssid = "Blanked out";
const char* password = "Blanked out";

int gpio13Led = 13;
int gpio12Relay = 12;

void setup(void){
// preparing GPIOs
pinMode(gpio13Led, OUTPUT);
digitalWrite(gpio13Led, HIGH);

pinMode(gpio12Relay, OUTPUT);
digitalWrite(gpio12Relay, HIGH);

Serial.begin(115200);
delay(5000);
WiFi.begin(ssid, password);
Serial.println("");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
}
void loop(void){
Serial.println("Wifi Up - Test Start!");
delay(10000);
if((WiFi.status() != WL_CONNECTED)) {
Serial.println("wifi down");
return;
}

HTTPClient http;
//Here I gave the footprint along with the url. But I'm not sure if it works.
http.begin("https://developer-api.nest.com/", "87:CB:F2:E6:44:C0:AA:F2:4C:28:B2:97:85:70:18:92:45:1B:A4:57");

//http.addHeader("Cache-Control", "no-cache", true, true);
//http.addHeader("Postman-Token", "419da1fa-6070-3338-c215-8be740891136", true, true);
http.addHeader("Authorization", "Bearer c.blanked out", true, true);
http.addHeader("Host", "developer-api.nest.com", true, true);
http.addHeader("Content-Type", "application/json", false, true);



int httpCode = http.GET();   
Serial.println("request sent: \n");
Serial.println(httpCode); //Doesn't seem to work? I never see any outprint.
if (httpCode == HTTP_CODE_OK) {
   String payload = http.getString();
   Serial.printf("[HTTP] GET response: %s", payload.c_str());
 } else {
   String payload = http.getString();
   String loc = http.header("Location");
   Serial.printf("[HTTP] GE returned %d. kapot, error: %s; response: %s, new location: %s\n", httpCode, http.errorToString(httpCode).c_str(), payload.c_str(), loc.c_str());
   
 }

 http.end();

}




I would really appreciate some feedback.
For the people interested, I've also asked on github, but without any response so far... :cry: :cry: :cry: :cry: :cry:
https://github.com/esp8266/Arduino/issues/4300

Re: Https problem with nest_api in http library?

PostPosted: Sun Feb 11, 2018 2:20 am
by tele_player
#include <WiFiClientSecure.h>

You need to use this library. Search for examples.

Re: Https problem with nest_api in http library?

PostPosted: Sun Feb 11, 2018 8:22 am
by pingwing
Hi,
Thanks for the response! Weird that I didn't find this after extensive searches on different kind of forums and search machines. I'll keep post my result if I got some better results. I'm curious already.

Re: Https problem with nest_api in http library?

PostPosted: Sun Feb 11, 2018 4:20 pm
by pingwing
Great!
I've altered my cod, but not with much of success. I'm pretty sure there's a bug somewhere. But I can't see it.
On postman my get request still works. So where am I going wrong?



Code: Select all/*
 *  HTTP over TLS (HTTPS) example sketch
 *
 *  This example demonstrates how to use
 *  WiFiClientSecure class to access HTTPS API.
 *  We fetch and display the status of
 *  esp8266/Arduino project continuous integration
 *  build.
 *
 *  Limitations:
 *    only RSA certificates
 *    no support of Perfect Forward Secrecy (PFS)
 *    TLSv1.2 is supported since version 2.4.0-rc1
 *
 *  Created by Ivan Grokhotkov, 2015.
 *  This example is in public domain.
 */

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "xxxxxx";
const char* password = "xxxxxxx";

const char* host = "developer-api.nest.com";
const int httpsPort = 443;

//declaring GPIO's
int gpio13Led = 13;
int gpio12Relay = 12;

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char* fingerprint = "87:CB:F2:E6:44:C0:AA:F2:4C:28:B2:97:85:70:18:92:45:1B:A4:57";

void setup() {
// preparing GPIOs
  pinMode(gpio13Led, OUTPUT);
  digitalWrite(gpio13Led, HIGH);
 
  pinMode(gpio12Relay, OUTPUT);
  digitalWrite(gpio12Relay, HIGH);
 
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

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

  String url = "https://developer-api.nest.com";
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTPS/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Authorization: Bearer c.xxxxxbgw2nsT85dJEHRpwvR7rSyrxxxxxxzB5PV8OtGDPxxxxxxmS\r\n" +
               "Content-Type: application/json\r\n\r\n");

  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  String line = client.readStringUntil('\n');
  if (line.startsWith("{\"state\":\"success\"")) {
    Serial.println("esp8266/Nest_api successfull!");
  } else {
    Serial.println("esp8266/nest_api has failed");

  }
  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");
}

void loop() {
}



The messages I've received are:
to developer-api.nest.com
certificate matches
requesting URL: https://developer-api.nest.com
request sent
headers received
esp8266/nest_api has failed
reply was:
==========

==========
closing connection

for the url I've used: https://developer-api.nest.com
and /developer-api.nest.com

I'm not entirely sure what to use there.

Is there also a way to see what you sent? That would also help me a lot to debug.

Thx a lot in advance