Python接收Google云端硬盘推送通知

时间:2016-07-22 16:54:00

标签: push-notification google-drive-api

自Drive SDK v3开始,只要文件发生变化,我们就可以从Google云端硬盘收到push notifications。目前我正在使用Python开发一个Drive应用程序,我希望收到这样的通知。我是否真的需要一个Web服务器,或者我可以使用套接字或类似的东西来实现它吗?

我知道我可以通过轮询changes.list方法来获得更改,但我想避免这种情况,因为有太多的API调用。如果文件发生变化,是否有更好的方法可以获得通知?

编辑:我抓住了我的网络流量,看到原始的Google Drive Client for Windows使用推送通知。因此,在某种程度上,必须能够在桌面应用程序中获取推送通知,但这可能是某种 Google magic ,我们无法将其与当前API一起使用

1 个答案:

答案 0 :(得分:2)

对于需要跟踪文件更改的Google Drive个应用,Changes collection提供了一种有效的方法来检测所有文件的更改,包括已与用户共享的文件。当且仅当文件自给定时间点以来已更改时,该集合通过提供每个文件的当前状态来工作。

检索更改需要pageToken来指示从中获取更改的时间点。

# Begin with our last saved start token for this user or the
# current token from getStartPageToken()
page_token = saved_start_page_token;
while page_token is not None:
response = drive_service.changes().list(pageToken=page_token,
fields='*',
spaces='drive').execute()
for change in response.get('changes'):
# Process change
print 'Change found for file: %s' % change.get('fileId')
if 'newStartPageToken' in response:
# Last page, save this token for the next polling interval
saved_start_page_token = response.get('newStartPageToken')
page_token = response.get('nextPageToken')