Cherrypy App Server to receive PUSH notifications from Google Drive API

80 views
Skip to first unread message

Alex S.

unread,
Mar 1, 2021, 11:58:35 AM3/1/21
to cherrypy-users
Hello guys, I'm stuck trying to sync Google drive PUSH notifications with my cherrypy App Server.

Here is my Cherrypy Server which functions normal when it reives JSON format
Response 200

#!/usr/bin/python3
 
import cherrypy
import json
import ws_proc
 
PORT = 9000
 
p = ws_proc.Ws_proc()
 
def cors():
    if cherrypy.request.method == 'OPTIONS':
        # preflign request  
        # see http://www.w3.org/TR/cors/#cross-origin-request-with-preflight-0
        cherrypy.response.headers['Access-Control-Allow-Methods'] = 'POST'
        cherrypy.response.headers['Access-Control-Allow-Headers'] = 'content-type'
        cherrypy.response.headers['Access-Control-Allow-Origin']  = '*'
        # tell CherryPy no avoid normal handler
        return True
    else:
        cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'
 
cherrypy.tools.cors = cherrypy._cptools.HandlerTool(cors)
 
@cherrypy.expose
class Greetings(object):
    def GET(self):
        return 'Welcome to CloudBackOffice.team Webservice'
 
@cherrypy.expose
@cherrypy.config(**{'tools.cors.on': True})
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
class POSTHandlerWebService(object):
    def POST(self):
        data = cherrypy.request.json
        return data
 
 
 
if __name__ == '__main__':
 
    # Global config
    cherrypy.config.update({'server.socket_port': PORT,
                            'server.socket_host': '127.0.0.1'})
 
 
    # Web Service config
    conf = {
        '/': {
            'response.timeout': 6000,
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.sessions.on': True
            }   
          }
 
    root = Greetings()
    root.send = POSTHandlerWebService()
    cherrypy.quickstart(root, config=conf)


The problem is when attempting to sync with the PUSH notifications it sends a POST message with this format:

POST https://mydomain.com/notifications // Your receiving URL. X-Goog-Channel-ID: channel-ID-value X-Goog-Channel-Token: channel-token-value X-Goog-Channel-Expiration: expiration-date-and-time // In human-readable format; present only if channel expires. X-Goog-Resource-ID: identifier-for-the-watched-resource X-Goog-Resource-URI: version-specific-URI-of-the-watched-resource X-Goog-Resource-State: sync X-Goog-Message-Number: 1

Response 415

Somebody can help on clarify why it isn't working.

Sviatoslav Sydorenko

unread,
Mar 1, 2021, 12:21:45 PM3/1/21
to cherryp...@googlegroups.com
Hey Alex,

пн, 1 бер. 2021 о 17:58 Alex S. <aseng...@gmail.com> пише:
> POST https://mydomain.com/notifications // Your receiving URL.
> X-Goog-Channel-ID: channel-ID-value X-Goog-Channel-Token:
> channel-token-value X-Goog-Channel-Expiration:
> expiration-date-and-time // In human-readable format; present
> only if channel expires. X-Goog-Resource-ID:
> identifier-for-the-watched-resource X-Goog-Resource-URI:
> version-specific-URI-of-the-watched-resource
> X-Goog-Resource-State: sync X-Goog-Message-Number: 1
>
> Response 415
>
> Somebody can help on clarify why it isn't working.

You need to record the network traffic of that HTTP request to be
able to figure out what's going on.
But the HTTP response of 415 Unsupported Media Type[1] likely
means that the Content-Type in their request is not `application/json`[2].

By the way, have you tried cherrypy-cors[3] for the CORS setup?

[1]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/415
[2]: https://github.com/cherrypy/cherrypy/blob/98929b5/cherrypy/lib/jsontools.py#L16
[3]: https://stackoverflow.com/a/57653463/595220


--
Cheers,
Sviatoslav.

---
https://useplaintext.email/
() ascii ribbon campaign - against html e-mail
/\ www.asciiribbon.org - against proprietary attachments
---

Tim Roberts

unread,
Mar 1, 2021, 12:32:00 PM3/1/21
to cherryp...@googlegroups.com
Alex S. wrote:
> Hello guys, I'm stuck trying to sync Google drive PUSH notifications
> with my cherrypy App Server.
>
> Here is my Cherrypy Server which functions normal when it reives JSON
> format
> ...
> The problem is when attempting to sync with the PUSH notifications it
> sends a POST message with this format:

I'm confused.  Are you saying YOUR server receives that POST request,
and responds with a 415?

The notification sync messages don't have any input or output. There is
no JSON for you to read, and you're not expected to send anything back.

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.


Alex S.

unread,
Mar 1, 2021, 12:45:29 PM3/1/21
to cherryp...@googlegroups.com
Hi Sviatoslav,

This is the answer from App server when sending the request as the sync message from Google Drive API using a testing tool

<Response [415]>
{'Server': 'nginx/1.18.0', 'Date': 'Mon, 01 Mar 2021 16:47:10 GMT', 'Content-Type': 'text/html;charset=utf-8', 'Content-Length': '1734', 'Connection': 'keep-alive', 'Keep-Alive': 'timeout=60', 'Allow': 'POST', 'Set-Cookie': 'session_id=0a04df85ab66c695d3213fc15df91d1fd3b7ce5c; expires=Mon, 01 Mar 2021 17:47:10 GMT; Max-Age=3600; Path=/'}

Sure I'm expecting an application/json request but for google drive push notifications it sends both application/json and text/html

I tested using cherrypy-cors but something is misconfigured.

I'm really stuck at this point, after a week of configuring this. I willtry again with the cherrypy-cors tool.

A. Senges
--
Just a human among others in the Uncanny valley.


--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/CAFYONRCKj5vK6C0dCPyc6L4uSVsqYffy-tDMTkTxuZ9BK5dbRw%40mail.gmail.com.

Sviatoslav Sydorenko

unread,
Mar 1, 2021, 12:56:50 PM3/1/21
to cherryp...@googlegroups.com
пн, 1 бер. 2021 о 18:45 Alex S. <aseng...@gmail.com> пише:
> This is the answer from App server when sending the request as the sync message from Google Drive API using a testing tool
>
> <Response [415]>
> {'Server': 'nginx/1.18.0', 'Date': 'Mon, 01 Mar 2021 16:47:10 GMT', 'Content-Type': 'text/html;charset=utf-8', 'Content-Length': '1734', 'Connection': 'keep-alive', 'Keep-Alive': 'timeout=60', 'Allow': 'POST', 'Set-Cookie': 'session_id=0a04df85ab66c695d3213fc15df91d1fd3b7ce5c; expires=Mon, 01 Mar 2021 17:47:10 GMT; Max-Age=3600; Path=/'}
>
> Sure I'm expecting an application/json request but for google drive push notifications it sends both application/json and text/html
>

I don't think the response is important. We've already determined that
`json_in` causes HTTP 415 because the incoming request does not
use proper `Content-Type` headers. You can replace the allowed type
values by changing the `content_type` setting of `json_in`. But in the
light of what Tim wrote above, it sounds that you shouldn't expect
JSON at all.

I haven't used this service but according to
https://developers.google.com/drive/api/v3/push#examples,
the body can be missing. You should probably try just accepting
raw requests with an undecorated handler, log that and act
accordingly.

Alex S.

unread,
Mar 2, 2021, 3:30:17 AM3/2/21
to cherryp...@googlegroups.com
Hey guys now responding 200

I attended your advice Tim and Sviatoslav.

So PUSH notifications aren't JSON as I was expecting. 

127.0.0.1 - - [01/Mar/2021:16:56:52] "POST /send HTTP/1.1" 200 684 "" "python-requests/2.24.0"
127.0.0.1 - - [01/Mar/2021:16:58:18] "POST /send HTTP/1.1" 200 861 "" "APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)"

The first one is from my testing tool and the second one from google I guess is the sync message.

Thanks a lot!

But for now I open and edit a file in my drive but nothing arrives to notify me. What am I doing wrong?

A. Senges
--
Just a human among others in the Uncanny valley.


On Mon, Mar 1, 2021 at 5:34 PM Alex S. <aseng...@gmail.com> wrote:
Well I was expecting JSON because the docs explains that after created the channel with the Drive API it returns a message with this format:


POST https://mydomain.com/notifications // Your receiving URL.
X-Goog-Channel-ID: channel-ID-value
X-Goog-Channel-Token: channel-token-value
X-Goog-Channel-Expiration: expiration-date-and-time // In human-readable format; present only if channel expires.
X-Goog-Resource-ID: identifier-for-the-watched-resource
X-Goog-Resource-URI: version-specific-URI-of-the-watched-resource
X-Goog-Resource-State: sync
X-Goog-Message-Number: 1


But when sending PUSH notifiactions the format is JSON with or without body

POST https://mydomain.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" }

The first message is for sync purpose but I dont know how to handle both formats in the same endpoint.

Please refer to https://developers.google.com/drive/api/v3/push


A. Senges
--
Just a human among others in the Uncanny valley.

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-user...@googlegroups.com.

Alex S.

unread,
Mar 2, 2021, 3:30:17 AM3/2/21
to cherryp...@googlegroups.com
Well I was expecting JSON because the docs explains that after created the channel with the Drive API it returns a message with this format:

POST https://mydomain.com/notifications // Your receiving URL.
X-Goog-Channel-ID: channel-ID-value
X-Goog-Channel-Token: channel-token-value
X-Goog-Channel-Expiration: expiration-date-and-time // In human-readable format; present only if channel expires.
X-Goog-Resource-ID: identifier-for-the-watched-resource
X-Goog-Resource-URI: version-specific-URI-of-the-watched-resource
X-Goog-Resource-State: sync
X-Goog-Message-Number: 1


But when sending PUSH notifiactions the format is JSON with or without body

POST https://mydomain.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"
}

The first message is for sync purpose but I dont know how to handle both formats in the same endpoint.

Please refer to https://developers.google.com/drive/api/v3/push


A. Senges
--
Just a human among others in the Uncanny valley.

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-user...@googlegroups.com.

Tim Roberts

unread,
Mar 2, 2021, 12:09:42 PM3/2/21
to cherryp...@googlegroups.com
Alex S. wrote:
>
> But for now I open and edit a file in my drive but nothing arrives to
> notify me. What am I doing wrong?

What you're doing wrong is expecting a notification.  The push
notifications are only for when a resource CHANGES.  Opening a file
doesn't change the resource.

Alex S.

unread,
Mar 2, 2021, 12:19:23 PM3/2/21
to cherryp...@googlegroups.com
Hello Tim, I've edited files, even delete files, but nothing arrives. I guess it's something about response 200  to indicate success to the drive api. But when returning an object response, I am having an encode error. I am still trying to synchronize.


A. Senges
--
Just a human among others in the Uncanny valley.

--
You received this message because you are subscribed to a topic in the Google Groups "cherrypy-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cherrypy-users/aTXXTDLYVUE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cherrypy-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/3ff91478-51a0-67c3-f968-3271767d178e%40probo.com.
Reply all
Reply to author
Forward
0 new messages