2016-07-22 40 views
3

由於Drive SDK v3我們能夠在文件發生更改時從Google Drive接收​​。目前,我正在使用Python開發Drive應用程序,並希望收到此類通知。我真的需要一個Web服務器嗎?或者我可以用套接字或類似的東西來實現這個嗎?Python收到Google Drive推送通知

我知道我可以通過輪詢changes.list方法來獲得更改,但我想避免這種情況,因爲有太多的API調用。如果文件發生了變化,是否有更好的方法可以獲得通知?

編輯:我捕獲了我的網絡流量,並看到原始的Google Drive Client for Windows使用推送通知。因此,在某些方面它必須能夠得到推在桌面應用程序的通知,但這個也許某種谷歌神奇這是我們無法用當前API使用

回答

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') 
+0

我的意思是用_polling changes.list method_,我想避免這種情況,因爲循環中有太多的API調用。 – Cilenco