Receiving Google Drive Push Notifications - php

I have been able to establish a channel to receive push notifications from Google Drive by using the method described here Not receiving webhook notification from drive, why?. I am receiving notifications and everything is working fine. My problem is that when I receive the push notifications, I am only getting this information:
Content-Length: 0
Accept: */*
Accept-Encoding: gzip,deflate,br
Connection: Keep-alive
Host: www.domain.com
User-Agent: APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)
X-Goog-Channel-Expiration: Thu, 29 Dec 2016 00:00:00 GMT
X-Goog-Channel-Id: 01ecb23c-e718-8674-6ab3-623931741334
X-Goog-Message-Number: 2745870
X-Goog-Resource-Id: hw75x654x56jYhRNkfU5CFEXXXhtlj8
X-Goog-Resource-State: change
X-Goog-Resource-Uri: https://www.googleapis.com/drive/v3/changes?includeRemoved=true&pageSize=100&pageToken=658&restrictToMyDrive=false&spaces=drive&alt=json
According to this documentation, there are some "Change" notifications messages that include a request body. Unfortunately, I have not been able to get the request body.
The script that handles the push notifications has the following logic:
$oldcontent = file_get_contents('notifications.txt');
$newnotsfile = fopen("notifications.txt", "w");
$post = file_get_contents('php://input');
$requestBody = json_decode($post , TRUE); //convert JSON into array
$time = date("Y-M-d H:i:s", time());
fwrite($newnotsfile , "<br><br>---------------- │ Time: ".$time."<br><br>");
foreach (getallheaders() as $name => $value) {
fwrite($newnotsfile , $name.": ".$value."<br>");
}
fwrite($newnotsfile , $requestBody );
fwrite($newnotsfile , "<br><br>");
fwrite($newnotsfile , $oldcontent);
fclose($newnotsfile );
?>
I thought that by using $post = file_get_contents('php://input'); I would capture the request body but the truth is that it is capturing nothing. If I understand correct, I should receive a change resource with the structure detailed here. Is there something wrong that I'm doing or have I understood this wrong? I appreciate any insight that can be given and thanks in advance!

Actually there is no request body which gets sent in the webhook notification. So as soon as changes arrive in the callback url, changes are to be fetched by making a get request to changes resource uri like below
Resource URI : https://www.googleapis.com/drive/v3/changes?includeRemoved=true&pageSize=100&pageToken=895&restrictToMyDrive=false&spaces=drive&alt=json
Or programatically changes can be fetched by using the below code
String pageToken = channelInfo.getCurrPageToken();
List<Change> changes = service.changes().list(pageToken)
.execute().getChanges();
Google push notifications doc could have mentioned this clearly rather than mentioning that the changes come along in the request body which is the reason for confusion

You might want to check the documentation - Push Notifications, this describes how to use push notifications that inform your application when a resource changes.
Watch response
If the watch request successfully creates a notification channel, it returns an HTTP 200 OK status code.
The message body of the watch response provides information about the notification channel you just created, as shown in the example below.
{
"kind": "api#channel",
"id": "01234567-89ab-cdef-0123456789ab"", // ID you specified for this channel.
"resourceId": "o3hgv1538sdjfh", // ID of the watched resource.
"resourceUri": "https://www.googleapis.com/drive/v3/files/o3hgv1538sdjfh", // Version-specific ID of the watched resource.
"token": "target=myApp-myFilesChannelDest", // Present only if one was provided.
"expiration": 1426325213000, // Actual expiration time as Unix timestamp (in ms), if applicable.
}
And if you will check the Understanding the notification message format:
Notification messages for Files and Changes are empty.
The docs also provided samples:
Change notification message for Files resources, which does not include a request body:
POST https://example.com/notifications // Your receiving URL.
Content-Type: application/json; utf-8
Content-Length: 0
X-Goog-Channel-ID: 4ba78bf0-6a47-11e2-bcfd-0800200c9a66
X-Goog-Channel-Token: 398348u3tu83ut8uu38
X-Goog-Channel-Expiration: Tue, 19 Nov 2013 01:13:52 GMT
X-Goog-Resource-ID: ret08u3rv24htgh289g
X-Goog-Resource-URI: https://www.googleapis.com/drive/v3/files/ret08u3rv24htgh289g
X-Goog-Resource-State: update
X-Goog-Changed: content,properties
X-Goog-Message-Number: 10
Change notification message for Changes resources, which includes a request body:
POST https://example.com/notifications // Your receiving URL.
Content-Type: application/json; utf-8
Content-Length: 118
X-Goog-Channel-ID: 8bd90be9-3a58-3122-ab43-9823188a5b43
X-Goog-Channel-Token: 245t1234tt83trrt333
X-Goog-Channel-Expiration: Tue, 19 Nov 2013 01:13:52 GMT
X-Goog-Resource-ID: ret987df98743md8g
X-Goog-Resource-URI: https://www.googleapis.com/drive/v3/changes
X-Goog-Resource-State: changed
X-Goog-Message-Number: 23
{
"kind": "drive#changes"
}
Understanding Drive API notification events
This section provides details on the notification messages you can receive when using push notifications with the Drive API.
You can try out any of the events below at the Push Notifications Playground or download the source from GitHub.
Hope this information helps.

Related

FCM HTTP v1 Batch request won't work in PHP

I'm using currently the Firebase messaging with PHP. I was able to make it work with a single notification with PHP and cURL. I've read the documentation about making batch request and I've constructed the request string as follow:
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer ya29.xxxxxnY
POST /v1/projects/xxxxxxxx/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"cxxxxx3",
"data":{
"typeNoti":"paiement",
"idcompte":"admin",
"typecompte":"paiement"
},
"notification":{
"title":"Test1",
"body":"Notification de test 1"
}
}
}
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer yaxxxxxxY
POST /v1/projects/xxxxxx/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"eoxxxxxU1",
"data":{
"typeNoti":"paiement",
"idcompte":"compte2",
"typecompte":"paiement"
},
"notification":{
"title":"Test1",
"body":"Notification de test 1"
}
}
}
--subrequest_boundary--
This string is generated in a file called batch_request.txt as the documentation said. When trying to send the request with cURL I receive this error:
{
"error": {
"code": 400,
"message": "Failed to parse batch request, error: 0 items. Received batch body: (0) bytes redacted",
"status": "INVALID_ARGUMENT"
}
}
I've experienced a strange behavior also, which is when I construct that string manually (in PHP I use HEREDOC), the batch request does occurs with 200ok response. I've tried to find some hidden character but couldn't.
I know there is a library called firebase-php. The problem with it is that I cannot find a straight foreward example like this:
//authenticate with json file
//create new token if it doesn't exist or skip
//create notification array
//create data array
//send multi notifications to multiple devices
I actually use a for loop and mono notification (one device token, one message at a time). The problem with this is that not all notifications are sent.
I appreciate your help thanks!
I gathered the code from firebase-php docs. After installing the library with composer I get a lot of errors and a lot of missing files in Firebase directory inside vendor like Factory.php etc
My solution was to NOT USE firebase-php library due to the numerous bugs I've faced (or maybe misuses) neither to construct the REST request text file for batch request. I used instead the NodeJS SDK which worked like a charm. I'm running a javascript file from my php code using exec.

Issue with google calendar push notification

I tried implementing the google calendar push notification in my server.
Using the outhplayground, i was able to successfully subscribe to the service.
I am getting notifications to my registered url when a change takes place in the calendar.
The only issue is that the response that i receive doesnt have data. Its an empty response.
Could anyone tell me what the issue would be. I am using PHP code in the backend to access the request hitting my url.
authplayground code:
POST /calendar/v3/calendars/calendarname#gmail.com/events/watch HTTP/1.1
Host: www.googleapis.com
Content-length: 161
Content-type: application/json
Authorization: Bearer access_token
{
"id": "01234267-89a6-cdef-0123456789ab", // Your channel ID.
"type": "web_hook",
"address": "https://example.com/response" // Your receiving URL.
}
Code to accept request:
$json = file_get_contents('php://input');
$request = json_decode($json, true);
$post_request = $_POST;
$get_request = $_REQUEST;
As I was getting an empty response, i tried writing the code to accept any possible way.
Google sends the response in the headers as an array.
Try this:
$response = apache_request_headers();
if($response) {
print_r($response);
}else {
echo 'The apache_request_headers() did not find any headers.'; //Or google is not sending any.
}
You may also try:
getallheaders()
if the apache_request_headers did not work.
Testing this can be difficult. You may want to set up a log that sends any data your page gets to a table on your database so that you can go back and inspect to see what type of progress you are making.
You can get the values like this:
$channelId = $_SERVER['HTTP_X_GOOG_CHANNEL_ID'];
$resourceId = $_SERVER['HTTP_X_GOOG_RESOURCE_ID'];
if($channelId) {
file_put_contents('webhook.txt', $channelId.' ||| '.$resourceId);
}
You can get the different headers here: https://developers.google.com/calendar/v3/push#understanding-the-notification-message-format

Why isn't the Amazon MWS Order API giving me rate limiting headers?

tl;dr
Why are x-mws-quota-max, x-mws-quota-remaining and x-mws-quota-resetsOn headers always omitted from Amazon's response to my API calls?
We are receiving request throttling exceptions in our order importer and I can't seem to figure out why, or at least why we can't see any throttling headers.
We request the MWS Orders API. First we list orders with the ListOrders operation, then for each order we ListOrderItems operation to grab the item details, which we then use internally for things like stock and reports. We use the Marketplace Web Service Order PHP SDK (#version 2013-09-01) to do this and run it on a cron job that runs every 15 minutes, all day, with each request limiting the scope of orders its pulling to only retrieve ones since the last cron instance ran.
In the documentation about throttling and on the API documentation itself, it speaks about the header items to provide feedback on your usage. For example:
x-mws-quota-max: 3600
x-mws-quota-remaining: 10
x-mws-quota-resetsOn: Wed, 06 Mar 2013 19:07:58 GMT
However, none of our requests are returning this data. When I try getQuotaMax() and other getters from the ResponseHeaderMetadata object, they all return null. If I dump the curl request directly I get the following:
HTTP/1.1 200 OK
Server: Server
Date: Wed, 31 Jan 2018 10:30:20 GMT
Content-Type: text/xml
Content-Length: 182649
Connection: keep-alive
X-Amz-Date: Wed, 31 Jan 2018 10:30:20 GMT
x-amzn-Authorization: AAA SignedHeaders=X-Amz-Date, identity=com.amazon.aaa.MarketplaceWebServiceOrders.AndromedaControlService.amzn1.aaa.id.lwigtwr3h4inoeknjer76q4tl4.Default/1, Signed=true, Encrypted=false, Signature=M/AAAAexATrzvpAAAAAKJIK9mkwsBjPryIWcvnAAAAA=, Algorithm=HmacSHA256
x-mws-request-id: 9f4f29a2-aaaa-4eca-9c7a-8023bea06fab
x-mws-timestamp: 2018-01-31T10:30:19.677Z
x-mws-response-context: aaaaaLSsB3L83dPqsIYmrA3VnhhMZypSJ2yQ9sxK//zDuOBUUuCyYdPzCiDrgrrzqw/BJLbPVAo=
Vary: Accept-Encoding,User-Agent
<?xml version="1.0"?>
<ListOrdersResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
<ListOrdersResult>
<Orders>
...
The exceptions that are caught on the nightly cron jobs seem to confirm that a) throttling is the issue and b) we aren't receiving anything back:
Caught Exception: Request is throttled
Response Status Code: 503
Error Code: RequestThrottled
Error Type: Unknown
Request ID: 94a816fd-d7cb-aaaa-9c46-d97ef4b338b1
XML: <?xml version="1.0"?>
<ErrorResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
<Error>
<Type></Type>
<Code>RequestThrottled</Code>
<Message>Request is throttled</Message>
</Error>
<RequestID>94a816fd-d7cb-aaaa-9c46-d97ef4b338b1</RequestID>
</ErrorResponse>
ResponseHeaderMetadata: RequestId: 94a816fd-d7cb-44a5-9c46-d97ef4b338b1, ResponseContext: Yzi1erdrrVZuGBzvtiUxd/94+Mj6QcN9BJReTcsg+MkelvfcYETwNjBER3CWqNhmA87P6n7sTq8=, Timestamp: 2018-01-30T22:00:59.545Z, Quota Max: , Quota Remaining: , Quota Resets At:
We never experienced this problem before, but now we're are making more sales on Amazon it's inevitable. However, am I missing something obvious in terms of actually getting the throttling headers so I can actually put in place counter-measures?
As a side note I don't think an hourly request limit, in addition to throttling, applies here as it says it only applies to the Products, Reports, and Feeds APIs in the above linked throttling documentation. A final side note is that we're not using a proxy and connect directly from the PHP script to the Amazon servers.
Not sure about the PHP MWS client, but the C# one doesn't actually fetch these parameters from the ResponseHeader. So you need to modify the MWS client code yourself a bit to specifically get your hands on these response header parameters.
I imagine this could be true for the PHP client as well.
The changes that need to be made in the C# MWS client in order to get these response params are:
first get the client code from amazon;
create 3 new string properties on the [SomeAmazonOperationResponse].ResponseHeaderMetadata class, and these should correspond to the parameters you want to get : x-mws-quota-max, x-mws-quota-remaining and x-mws-quota-resetsOn; You could name the properties something like QuotaMax, QuotaRemaining and QuotaResetsOn;
modify the constructor of ResponseHeaderMetadata to receive and initialise these properties as well.
go to MarketplaceWebServiceClient.Invoke() method, identify the exact place where the HttpWebResponse is obtained, and when the ResponseHeaderMetadata object is initialised (probably a var called rhm), make sure you specify the new parameters in the ctor (and each of them should be obtained in a manner similar to this: httpResponse.GetResponseHeader("x-mws-quota-max") )
now you should have access to the new populated values of the parameters on your response
enjoy

How to push notification from rest client for testing purpose

Hi I am trying to push the notification from rest client to android app. I am doing like below -
URL - https://android.googleapis.com/gcm/send
Method - POST
Headers - Authorization: my_server_key
I always get this message
Status Code: 401 Unauthorized
Alternate-Protocol: 443:quic,p=0.002
Cache-Control: private, max-age=0
I am not sure what I am missing & last I am using correct server api key.
Please assist & thanks in advance.
Push notification uses Web Socket. You cannot do this using HTTP protocol unless you have a server side implementation set up to respond to your request through Web Socket.
I set this values in the header then it is working fine for me.
Below is the header key & value.
A) Authorization: & it value likes key=API_KEY
B) Content-Type: application/json
I hope you are done with your request but it will help to others
You have to send like this
In head
-------
Headder Value
Authorization key=your value
Content-Type application/json
In Body
-------
Something like this
{
"registration_ids" : ["Your id"],
"data" : {
"message":"Your message"
}
}
Hope it will help

CURL response different than response to request sent from browser

Attempting to submit a form with CURL, both via PHP and the command line. The response from the server consists of null content (the headers posted below).
When the same URL is submitted via a browser, the response consists of a proper webapge.
Have tried submitting the CURL request parameters via POST and GET via each of the following command line curl flags "-d" "-F" and "-G".
If the query string parameters are posted with "-d" flag, resulting header is:
HTTP/1.1 302 Moved Temporarily
Date: Thu, 02 Jun 2011 21:41:54 GMT
Server: Apache
Set-Cookie: JSESSIONID=DC5F435A96A353289F58593D54B89570; Path=/XXXXXXX
P3P: CP="CAO PSA OUR"
Location: http://www.XXXXXXXX.com/
Content-Length: 0
Connection: close
Content-Type: text/html;charset=UTF-8
Set-Cookie: XXXXXXXXXXXXXXXX=1318103232.20480.0000; path=/
If the query string parameters are posted with "-F" flag, the resulting header is:
HTTP/1.1 100 Continue
HTTP/1.1 500 Internal Server Error
Date: Thu, 02 Jun 2011 21:52:54 GMT
Server: Apache
Content-Length: 1677
Connection: close
Content-Type: text/html;charset=utf-8
Set-Cookie: XXXXXXXXXXXXXX=1318103232.20480.0000; path=/
Vary: Accept-Encoding
<html><head><title>Apache Tomcat/5.5.26 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server encountered an internal error () that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>javax.servlet.ServletException: Servlet execution threw an exception<br>
</pre></p><p><b>root cause</b> <pre>java.lang.NoClassDefFoundError: com/oreilly/servlet/multipart/MultipartParser<br>
com.corsis.tuesday.servlet.mp.MPRequest.<init>(MPRequest.java:27)<br>
com.corsis.tuesday.servlet.mp.MPRequest.<init>(MPRequest.java:21)<br>
com.corsis.tuesday.servlet.TuesdayServlet.doPost(TuesdayServlet.java:494)<br>
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)<br>
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)<br>
</pre></p><p><b>note</b> <u>The full stack trace of the root cause is available in the Apache Tomcat/5.5.26 logs.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/5.5.26</h3></body></html>
Questions:
What might cause a server to respond different depending on the nature of the CURL request.
How to successfully submit request via CURL?
HTTP/1.1 100 Continue
I had problems associated with this header before. Some servers simply do not understand it. Try this option to override Expect header.
curl_setopt( $curl_handle, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
To add to what Richard said, I have seen cases where servers check the User-Agent string and behave differently based on its value.
I have just had an experience with this and what fixed it was surprising. In my situation I was logging into a server so I could upload a file, have the server do work on it, and then download the new file. I did this in Chrome first and used the dev tools to capture over 100 HTTP requests in this simple transaction. Most are simply grabbing resources I don't need if I am trying to do all of this from the command line, so I filtered out only the ones I knew at a minimum I should need.
Initially this boiled down to a GET to set the cookie and log in with a username and password, a POST to upload the file, a POST to execute the work on the file, and a GET to retrieve the new file. I could not get the first POST to actually work though. The response from that POST is supposed to be information containing the upload ID, time uploaded, etc, but instead I was getting empty JSON lists even though the status was 200 OK.
I used CURL to spoof the requests from the browser exactly (copying the User-Agent, overriding Expect, etc) and was still getting nothing. Then I started arbitrarily adding in some of the requests that I captured from Chrome between the first GET and POST, and low and behold after adding in a GET request for the JSON history before the POST the POST actually returned what it was supposed to.
TL;DR Some websites require more requests after the initial log in before you can POST. I would try to capture a successful exchange between the server and browser and look at all of the requests. Some requests might not be as superfluous as the seem.

Categories