0

I am using ESP8266 programmed from Arduino to get data from the Google Youtube API. API, of course, works great in a browser using URL:

https://www.googleapis.com/youtube/v3/channels?part=statistics&id=CHANNEL-ID&key=GOOGLE-API-KEY

The problem is that "gooogleapis.com" is not the location where from I would receive the answer to my HTTPS request, it comes from a different location. When above URL is entered, it is being redirected and request answer comes from another location.

So what happens on ESP8266 when I try to open that address(of course, with added proper channel ID and google key) is following:

HTTP/1.1 404 Not Found
Content-Type: text/html; charset=UTF-8
Content-Length: 1580
Date: 2017年3月12日 14:40:18 GMT
Alt-Svc: quic=":443"; ma=2592000; v="36,35,34"
Connection: close
<!DOCTYPE html>
<html lang=en>
 <meta charset=utf-8>
 <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
 <title>Error 404 (Not Found)!!1</title>
 <style>
 *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
 </style>
 <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
 <p><b>404.</b> <ins>That’s an error.</ins>
 <p>The requested URL <code>/youtube/v3/channels</code> was not found on this server. <ins>That���s all we know.</ins>
Done.

Which is the Google error page(you seen it!). This is my Arduino code:

#include "ESP8266WiFi.h"
#include <WiFiClientSecure.h>
const char* ssid = "SSID"; // WiFi network name
const char* password = "pass"; // WiFi pass
const char* host = "googleapis.com"; // page I am trying to load
const char* fingerprint = "22 37 4D 58 43 F4 A1 24 12 71 2B 74 7A FC 36 FC 24 A0 F0 9D";
String ch_id = "CHANNEL ID";
String api_key = "API KEY";
void setup() {
 Serial.begin(115200); 
 delay(10);
 WiFi.begin(ssid, password); 
 while (WiFi.status() != WL_CONNECTED) { 
 delay(500); 
 Serial.print("."); 
 }
 Serial.println("Connected to WiFi! Local IP address is: ");
 Serial.println(WiFi.localIP());
 WiFiClientSecure client; 
 if (!client.connect(host, 443))
 {
 Serial.println("I am unable to connect to that page."); 
 return; 
 }
 if (client.verify(fingerprint, host)) {
 Serial.println("certificate OK");
 } else {
 Serial.println("certificate NOT OK");
 }
 String url = "/youtube/v3/channels?part=statistics&id=" + ch_id + "&key=" + api_key;
 client.print(String("GET ") + url + " HTTP/1.1\r\n" +
 "Host: " + host + "\r\n" +
 "Connection: close\r\n\r\n");
 delay(10);
 while (client.available()) {
 String linija = client.readStringUntil('\r'); 
 Serial.print(linija);
 }
 Serial.println("Done.");
}
void loop() { 
}
  • How to find out to which site I am redirected?
  • How to read that from ESP8266 and redirect the same data to new site?
asked Mar 12, 2017 at 14:43
1
  • 1
    404 is not a redirect. You should get a 302 or 301. In that case there will also be a Location: header that contains the new URL. Commented Mar 12, 2017 at 15:53

1 Answer 1

1

In your browser you used www.googleapis.com, but in your code you used googleapis.com.

https://googleapis.com/youtube/v3/channels?part=statistics&id=CHANNEL-ID&key=GOOGLE-API-KEY

returns a 404 page.

Add www. to your host in code.

answered Mar 13, 2017 at 13:31
1
  • Perfect! Can't believe I missed something so trivial. Thank you very much - works now. Commented Mar 13, 2017 at 13:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.