推送通知 – Python接收Google Drive推送通知

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

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

编辑:我捕获了我的网络流量,并看到原始的Google Drive Client for Windows使用推送通知.因此,在某种程度上,必须能够在桌面应用程序中获取推送通知,但这可能是某种Google魔术,我们无法使用当前的API

最佳答案 对于需要跟踪文件更改的
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')
点赞