如何使用python成功接收Google Drive v3推送通知?

2024-04-25 11:39:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经完成了Google提供的使用driveapi实现push notifications的步骤。一切都如前所述,除了我不会收到任何来自Google的通知,只要发生变化(如下面的步骤4所示)。在

以下是我遵循的步骤和每个阶段的结果的摘要:

步骤1(成功):验证您拥有该域

我按照指示去了搜索控制台,并成功地验证了这个网站。在

第2步(成功):注册您的域

我访问了域验证页面,并在API控制台中添加了我的域。googleapi控制台更新,显示我想要的域名列表。在

我有一个有效的SSL证书从GoDaddy为我的域,并已验证。在

步骤3(成功):创建了我的通知通道

使用pythonsdk,我创建了这个频道,并从Google得到了一个响应。下面的python代码展示了我是如何做到这一点的。注意:我的请求中包含的地址指示Google向我的Flask网站发送通知,我正在Google的App Engine中托管这些通知。我将域名显示为_域名.com这里是为了隐私,因为我正在进行网站建设。在

channel_id = str(uuid.uuid4())

body = {
    'kind': 'api#channel',
    'id': channel_id,
    'type': 'web_hook',
    'address': "https://my_domain.com/notifications"
}


try:
    start_page_token = service.changes().getStartPageToken().execute().get('startPageToken');
    print(service.changes().watch(pageToken=start_page_token, body=body).execute())
except Exception as e: 
    print('Exception: {}'.format(e))

以下是谷歌的回应:

^{pr2}$

步骤4(失败):处理来自Google的通知

我正在应用引擎上运行一个烧瓶网站,并创建了一个应用程序路径从谷歌接收通知。我已经包括了下面的代码。我从来没有收到任何在这个网址。在

@app.route('/notifications')
def notifications():
    print('in notifications()')

    try:
        chan_id = request.args.get('X-Goog-Channel-ID', 'empty')
        msg_num = request.args.get('X-Goog-Message-Number', 'empty')
        rid = request.args.get('X-Goog-Resource-ID', 'empty')
        state = request.args.get('X-Goog-Resource-State', 'empty')  
        resource_uri = request.args.get('X-Goog-Resource-URI', 'empty') 
        goog_changed = request.args.get('X-Goog-Changed', 'empty')
        goog_chan_exp = request.args.get('X-Goog-Channel-Expiration', 'empty')
        goog_chan_token = request.args.get('X-Goog-Channel-Token', 'empty')

        print('chan_id: {}'.format(chan_id))
        print('msg_num: {}'.format(msg_num))
        print('rid: {}'.format(rid))
        print('state: {}'.format(state))
        print('resource_uri: {}'.format(resource_uri))
        print('goog_changed: {}'.format(goog_changed))
        print('goog_chan_exp: {}'.format(goog_chan_exp))
        print('goog_chan_token: {}'.format(goog_chan_token))


    except Exception as e:
        print('notifications() exception: {}'.format(e))

    print('leaving notifications()')

    return jsonify(result='done')

Tags: tokenidformatget网站requestgoogleargs
1条回答
网友
1楼 · 发布于 2024-04-25 11:39:39

我不确定这是否是问题所在,但是,当我尝试googledrive推送通知时,我没有使用SDK。我只提出了一个POST请求,如文档中所示。在

你能试着向"https://www.googleapis.com/drive/v3/changes/watch"发出POST请求而不是使用service.changes().watch()吗?在

您将需要一个access_token作为请求,但我相信您不需要发送startPageToken,因为他们的示例中没有使用它: example

我希望这有帮助!在

相关问题 更多 >