Arduino make GET request to Google app engine - google-app-engine

I'm trying to make a get request from a Arduino to Google App Engine:
client.println("GET /getData HTTP/1.0\r\n");
client.println("HOST: xxxxxxxxxx.appspot.com\r\n");
client.println("\r\n");
And I get the following error message:
HTTP/1.0 404 Not Found
Content-Type: text/html; charset=UTF-8
Content-Length: 1576
Date: Tue, 24 May 2016 12:07:34 GMT
<!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>/getData </code> was not found on this server. <ins>That’s all we know.</ins>
I cant seem to get the formatting of the request right. Does anyone have a idea how to get it working? When I test it against WAMP server everything works fine. Thanks

This code should help you:
char server[]="your_server_name";
EthernetClient client;
IPAddress ip(192, 168, 1, 40); // Arduino IP Add
IPAddress myDns(8,8,8,8); // DNS
IPAddress myGateway(192,168,1,1); // your gateway
void setup() {
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac,ip,myDns,myGateway);
}
}
in the loop():
... loop funcion
if (client.connect(server,80)) // Server port
{
client.println("GET /getData HTTP/1.0\r\n");
client.println("HOST: host_name\r\n");
client.println("\r\n");
}

Related

sporadic error renaming a label with Gmail REST API -- known issue? workarounds?

We run a service that synchronizes with Gmail using the REST API. Since last Wednesday, some of our (unchanged) Gmail tests have begun sporadically failing. The cause appears to be on the Gmail REST API side.
One failing test involves creating a label, creating a message, tagging a message with the label, and then renaming the label. When the tests fail, the POST request to change the label name returns a 404 error. A GET on that label or on the label list, either before or after the first 404, will successfully return the label in question. Retrying the POST keeps resulting in a 404 for at least 30 seconds, even with a 5-second gap between retries. Adding a 15-second delay before trying the POST doesn’t help.
Is this a known issue? If so, are there any known workarounds? What can we provide to help diagnose and fix this? We’re using the Google-HTTP-Java-Client/1.22.0 client library.
Here's the request/response from a successful POST:
POST https://www.googleapis.com/gmail/v1/users/me/labels/Label_4311
Accept-Encoding: gzip
Authorization: <Not Logged>
User-Agent: pexlabs Google-API-Java-Client Google-HTTP-Java-Client/1.22.0 (gzip)
x-http-method-override: PATCH
Content-Type: application/json; charset=UTF-8
Content-Encoding: gzip
Content-Length: 38
{"name":"mystery"}
HTTP/1.1 204 No Content
Alt-Svc: quic=":443"; ma=2592000; v="36,35,34,33,32"
Server: GSE
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
ETag: "SJIjOPuAzi7meWvLIr4rJgaI0K4/vyGp6PvFo4RvsFtPoIWeCReyIC8"
Vary: X-Origin
Vary: Origin
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Pragma: no-cache
Date: Tue, 20 Sep 2016 15:30:51 GMT
Here's the request/response from a failed POST:
POST https://www.googleapis.com/gmail/v1/users/me/labels/Label_4317
Accept-Encoding: gzip
Authorization: <Not Logged>
User-Agent: pexlabs Google-API-Java-Client Google-HTTP-Java-Client/1.22.0 (gzip)
x-http-method-override: PATCH
Content-Type: application/json; charset=UTF-8
Content-Encoding: gzip
Content-Length: 38
{"name":"mystery"}
HTTP/1.1 404 Not Found
X-Frame-Options: SAMEORIGIN
Alt-Svc: quic=":443"; ma=2592000; v="36,35,34,33,32"
Server: ESF
X-XSS-Protection: 1; mode=block
Content-Length: 1596
Date: Tue, 20 Sep 2016 15:44:08 GMT
Content-Type: text/html; charset=UTF-8
<!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>/gmail/v1/users/me/labels/Label_4317</code> was not found on this server. <ins>That’s all we know.</ins>
Looking at a few runs, the Server response header value differs between successes and failures (though that may be by design).
This is a very peculiar error. Patching of labels seem to be broken. I also get Error 404 (Not Found)!!1. What on earth?!
In the mean time your could just get the label data, and then update the fields that can be changed, but just changing the name to your liking:
Request 1
GET https://www.googleapis.com/gmail/v1/users/me/labels/Label_203?access_token={access_token}
Response 1
{
"id": "Label_203",
"name": "test1234",
"messageListVisibility": "show",
"labelListVisibility": "labelShow",
"type": "user",
"messagesTotal": 0,
"messagesUnread": 0,
"threadsTotal": 0,
"threadsUnread": 0
}
Request 2
PUT https://www.googleapis.com/gmail/v1/users/me/labels/Label_203?access_token={access_token}
{
"id": "Label_203",
"labelListVisibility": "labelShow",
"messageListVisibility": "show",
"name": "test12345"
}
Response 2
{
"id": "Label_203",
"name": "test12345",
"messageListVisibility": "show",
"labelListVisibility": "labelShow"
}
Update
The API Explorer does not work for some odd reason. However, if you change from POST to PATCH in your request, it will work:
Request
PATCH https://www.googleapis.com/gmail/v1/users/me/labels/Label_203?access_token={access_token}
{
"name": "wow123"
}
Response
{
"id": "Label_203",
"name": "wow123",
"messageListVisibility": "show",
"labelListVisibility": "labelShow",
"type": "user",
"messagesTotal": 0,
"messagesUnread": 0,
"threadsTotal": 0,
"threadsUnread": 0
}

google cloud engine metadata query

I have set a metadata groupip="10.0.0.2 10.0.0.3 10.0.0.4" in the project metadata. I am trying to query it using the following command:
curl 'http://metadata/computeMetadata/v1/projectid/groupip/' -H "Metadata-Flavor: Google"
As answer instead of the IP's I am getting the following:
<!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}bod
y{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/im
ages/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;t
ext-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.c
om/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//w
ww.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}#media only screen and (-webkit-min-d
evice-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>/computeMetadata/v1/projectid/groupip/</code> was not found on this server. <ins>Th
at’s all we know.</ins>
In the same way that you get an instance's metadata via instance/attributes/xyz, so you can get the project's metadata via project/attributes/xyz.
curl http://metadata/computeMetadata/v1/project/attributes/groupip -H "Metadata-Flavor: Google"
To make sure you actually added the project-level metadata correctly, list all project attributes:
And more generally, keep shortening the URL to list ancestors:

Calling Google Apps Script as webapp from AppEngine returns 405

I have issues when I try to call a GAPPS script that is published as a webapp. I am trying to call this service from AppEngine. I am doing a POST method to this script with a JSON Payload. Urlfetch options are set to follow redirects and a timeout of 30 secs. The script is publicly available and open to anonymous users.
On my local GAE development machine, the URLFetch response from the script is 200, and all is fine.
When I move to production, the script gets executed but returns errors and an HTML page.
I have follow redirects on on URLFetch and I have set the deadline to 30secs.
status response : 405
response text :
<!DOCTYPE html><html lang="en" ><head><meta name="description" content="Web word processing, presentations and spreadsheets"><link rel="shortcut icon" href="//ssl.gstatic.com/docs/common/drive_favicon1.ico"><title>Google Drive -- Page Not Found</title><link href="//fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet" type="text/css"><style>/* Copyright 2014 Google Inc. All Rights Reserved. */
.goog-inline-block{position:relative;display:-moz-inline-box;display:inline-block}* html .goog-inline-block{display:inline}*:first-child+html .goog-inline-block{display:inline}#drive-logo{color:#91959c;font-family:"Open Sans",Arial,sans-serif;font-size:27px;font-weight:300;position:absolute;text-shadow:0 1px 1px white;white-space:nowrap}#drive-logo img{padding:0 0.4em 0 0;position:relative;top:2px;vertical-align:middle}#drive-logo a{color:#91959c;text-decoration:none}#drive-logo span.goog-inline-block{margin-top:2px;vertical-align:top}</style><style type="text/css">body {background-color: #fff; font-family: Arial,sans-serif; font-size: 13px; margin: 0; padding: 0;}a, a:link, a:visited {color: #112ABB;}</style><style type="text/css">.errorMessage {font-size: 12pt; font-weight: bold; line-height: 150%;}</style></head><body><div style="margin: auto; max-width: 750px;"><div style="margin: 80px 40px 20px 40px; position:relative; "><div style="position: absolute; top: -80px;"><h1 id="drive-logo"><img src="//www.google.com/images/logos/google_logo_41.png" width="116" height="41" alt="Google logo" ><span class="goog-inline-block">Drive</span></h1></div><div align="center"><p class="errorMessage" style="padding-top: 50px">Sorry, the file you have requested does not exist.</p><p> Please check the address and try again. </p><div style="background: #F0F6FF; border: 1px solid black; margin-top: 35px; padding: 10px 125px; width: 300px;"><p><strong>Get stuff done with Google Drive</strong></p><p>Apps in Google Drive make it easy to create, store and share online documents, spreadsheets, presentations and more.</p><p>Learn more at drive.google.com/start/apps.</p></div></div></div></div></body></html>
response header:Expires=Fri, 01 Jan 1990 00:00:00 GMT
response header:X-XSS-Protection=1; mode=block
response header:Alternate-Protocol=443:quic,p=0.02
response header:Set-Cookie=NID=67=KSieONzrWKqWVEt8E4M2rUo_2nooEJ6qrh80LXTppGKYQux2iGFN-BHoqn7r5Yn0F4WaYtTbXDa6y2iAa3FtRBMAn6qf526lmPkZZek7nSQUvCv4wvthsDsL4P11Yx;Domain=.googleusercontent.com;Path=/;Expires=Tue, 02-Jun-2015 23:39:38 GMT;HttpOnly
response header:X-Google-Cache-Control=remote-fetch
response header:Server=GSE
response header:Cache-Control=no-cache, no-store, max-age=0, must-revalidate
response header:Pragma=no-cache
response header:X-Content-Type-Options=nosniff
response header:X-Frame-Options=SAMEORIGIN
response header:Date=Mon, 01 Dec 2014 23:39:38 GMT
response header:P3P=CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
response header:Allow=HEAD, GET
response header:x-chromium-appcache-fallback-override=disallow-fallback
response header:Via=HTTP/1.1 GWA
response header:Content-Type=text/html; charset=utf-8
The funny - no so much - thing is that the script WORKS NEVERTHELESS most of the time but not always or not for all users.
You can see that thet response header says Allow=HEAD, GET but I implemented doPOst in my script.
Nevertheless, I have tried solving this problem by rewriting the whole things to work with GET and a parameter but I am running into similar issues !
Long story short : works perfectly and with status response 200 on GAE local development server, faulty responses and unstable behavior when deployed to GAE>
GAS is very picky when running as webapp. Sometimes it helps to put the script into a public folder on Google Drive.

Parse MIME Body using Java Mail MimeMessage

How to parse mime body part using Java Mail MimeMessage, I am fetching BODY part alone from my IMAP server.
Command I used to fetch BODY part alone.
A001 UID FETCH 1 (UID FLAGS BODY.PEEK[1])
This will fetch raw MIME message without headers and attachments ( but contains inline images ).
When I parse the fetched content using Java Mime Message, I am getting wrong result.
For Example : If the raw MIME contains inline images part.getInputStream() return content with inline image data
Raw MIME:
------=_Part_385483_1716430164.1405422119116
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
Test mail
------=_Part_385483_1716430164.1405422119116
Content-Type: multipart/related;
boundary="----=_Part_385484_590068567.1405422119140"
------=_Part_385484_590068567.1405422119140
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta content="text/html;charset=UTF-8" http-equiv="Content-Type"></head><body ><div style='font-size:10pt;font-family:Verdana,Arial,Helvetica,sans-serif;'>Test mail<br><img src="cid:inline_img" style="height: 1200px; width: 1600px;"></body></html>
------=_Part_385484_590068567.1405422119140
Content-Type: image/jpeg; name=1405422097638.jpeg
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=1405422097638.jpeg
Content-ID: <inline_img>
/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4ADkFkb2JlAGTAAAAAAf/b
AIQABgQEBAUEBgUFBgkGBQYJCwgGBggLDAoKCwoKDBAMDAwMDAwQDA4PEA8ODBMTFBQTExwbGxsc
Hx8fHx8fHx8fHwEHBwcNDA0YEBAYGhURFRofHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8f
Hx8fHx8fHx8fHx8fHx8fHx8f/8AAEQgEsAZAAwERAAIRAQMRAf/EAMUAAQADAQEBAQEBAAAAAAAA
.....
------=_Part_385484_590068567.1405422119140--
------=_Part_385483_1716430164.1405422119116--
Result:
Test mail
------=_Part_385483_1716430164.1405422119116
Content-Type: multipart/related;
boundary="----=_Part_385484_590068567.1405422119140"
------=_Part_385484_590068567.1405422119140
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta content="text/html;charset=UTF-8" http-equiv="Content-Type"></head><body ><div style='font-size:10pt;font-family:Verdana,Arial,Helvetica,sans-serif;'>Test mail<br><img src="cid:inline_img" style="height: 1200px; width: 1600px;"></body></html>
------=_Part_385484_590068567.1405422119140
Content-Type: image/jpeg; name=1405422097638.jpeg
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=1405422097638.jpeg
Content-ID: <inline_img>
/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4ADkFkb2JlAGTAAAAAAf/b
AIQABgQEBAUEBgUFBgkGBQYJCwgGBggLDAoKCwoKDBAMDAwMDAwQDA4PEA8ODBMTFBQTExwbGxsc
Hx8fHx8fHx8fHwEHBwcNDA0YEBAYGhURFRofHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8f
Hx8fHx8fHx8fHx8fHx8fHx8f/8AAEQgEsAZAAwERAAIRAQMRAf/EAMUAAQADAQEBAQEBAAAAAAAA
.....
------=_Part_385484_590068567.1405422119140--
------=_Part_385483_1716430164.1405422119116--
Can any one suggest how to parse body using Java MimeMessage.
Thanks.
You are getting just the .TEXT portion (i.e. the content of the part), but you need to combine the .MIME and the .TEXT before you'll be able to parse it.
You can see how I do this in my own IMAP library (written in C#, though) in the GetBodyPart method of ImapFolder.cs:
https://github.com/jstedfast/MailKit/blob/master/MailKit/Net/Imap/ImapFolder.cs#L3729
Effectively I request <part-spec>.MIME and <part-spec>.TEXT and then chain them together in a custom ChainedStream class that takes a list of streams and reads from them as if they were a single, sequential, stream.
Are you implementing the IMAP protocol support yourself instead of using the IMAP protocol support built in to JavaMail? If so, you need to spend a lot more time reading the IMAP protocol spec!
To get the entire message content, leave out the section id - BODY.PEEK[].

Declaring mime type for html-email

I want to create a html email and I've read a lot about how to do it. There is one piece of information I can't find. How should I declare the mime type? I tried with:
meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
but it doesn't work.
Later edit:
I am trying to set the content-type of the mail to text/html but I don't know how. All this when writing from a regular email client. I have to declare it in the mail body? Or in the mail header (if so, how do I o it?)?
Are you trying to set the content-type declaration within the message header sent to the mail server? If so, you should set it this way, in a line itself:
Content-Type: text/html; charset=UTF-8
The end tag for meta tag is used only in xhtml/xml. If you are using html, you should use it inside <head> tags like:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
Basically email clients ignore any META tags with Content type in them (at least as of 2013-10-17).
You need to set a the content type declaration in a special header in the email server.
More information about this issue can be found at http://www.emailonacid.com/blog/details/C13/the_importance_of_content-type_character_encoding_in_html_emails
If this makes no sense to you, then I'm afraid you're out of luck. The only reliable solution I've found is to convert any special characters to their HTML entity equivalent. The link above has a link to a tool that does this for you.
Hope that helps!
This applies to php:
// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: Mary <mary#example.com>, Kelly <kelly#example.com>';
$headers[] = 'From: Birthday Reminder <birthday#example.com>';
$headers[] = 'Cc: birthdayarchive#example.com';
$headers[] = 'Bcc: birthdaycheck#example.com';
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
http://php.net/manual/en/function.mail.php#example-4180

Resources