自 Drive SDK v3 以来,我们能够收到 push notifications每当文件更改时从 Google Drive 中获取。目前我正在使用 Python 开发 Drive 应用程序,我希望收到这样的通知。我真的需要一个网络服务器吗?或者我可以用一个套接字或类似的东西来实现它吗?
我知道我可以通过轮询 changes.list 来获得更改。方法,但我想避免这种情况,因为 API 调用太多。如果文件已更改,是否有更好的方法来获取通知?
编辑 :我捕获了我的网络流量,发现原来的 Windows 版 Google Drive 客户端使用推送通知。所以在某种程度上,它必须有可能在桌面应用程序中获得推送通知,但这可能是某种我们无法与当前 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')
关于push-notification - Python 接收 Google Drive 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38531725/