2015-02-24 138 views
2

我在我的网站上整合了谷歌驱动器API推送通知。 工作电流:开发者控制台,域等上如何获取Google Drive上已更改文件的ID

  1. 注册的Web应用程序...
  2. OAuth授权
  3. 如果不存在
  4. 设置的servlet创建信道等待谷歌驱动POST请求,如果发生变化
  5. 更改发生在谷歌驱动器(上传或删除)
  6. Servlet捕捉来自谷歌驱动器的POST请求发生变化时
  7. 缓存的消息:{ "kind": "drive#change", "id": "8664", "selfLink": "https://www.googleapis.com/drive/v2/changes/8664"}

谷歌更改文件的驱动器API表示:

{ 
"kind": "drive#change", 
    "id": long, 
    "fileId": string, 
    "selfLink": string, 
    "deleted": boolean, 
    "modificationDate": datetime, 
    "file": files Resource 
} 

如何获得FILEID?为什么Google云端硬盘不会向我发送缺少的字段?我究竟做错了什么?

关注变化代码:

service -> created ok 
channelId -> UUID 
channelType -> web_hook 
channelAddress -> https://example.com/GoogleDriveWebhook (GoogleDriveWebhook is servlet) 
public static Channel watchChange(Drive service, String channelId, String channelType, String channelAddress) { 
     Channel channel = new Channel(); 
     channel.setId(channelId); 
     channel.setType(channelType); 
     channel.setAddress(channelAddress); 
     channel.setExpiration(new Date().getTime() + (600000 * 2));//20 min session for channel (test environment) 
     try { 
      return service.changes().watch(channel).execute(); 
     } catch (IOException e) { 
      System.out.println("Something is wrong with instancing new channel"); 
      e.printStackTrace(); 
     } 
     return null; 
    } 

...所以很明显的应用程序捕获的变化,谷歌驱动器发送,我坚持plz帮助!

+0

你在寻找什么样的缺失参数?请检查此页面的响应资源:https://developers.google.com/drive/web/push – KRR 2015-02-24 23:22:14

+0

Thx我读它,所以这推送通知并没有真正告诉我什么文件被改变,只是通知我,已经改变了谷歌驱动器或? 然后,我尝试使用“Changes:get”API,使用参数:(Drive service,String changeId),其中我通过{“kind”:“drive#change”,**“id”:“8664”**,“selfLink “:”https://www.googleapis.com/drive/v2/changes/8664“} id像字符串changedId,但我总是得到”更改未找到:8754“上传或删除 – 2015-02-25 08:38:39

回答

1

发生通知时发送的POST消息将在头中具有文件ID(请参阅下面的“X-Goog-Resource-ID”)。如果你正在观看一个文件或文件夹,你甚至无法解析。

为文件资源变更通知消息,该消息不包括请求体:

POST https://example.com/notifications // Your receiving URL. 
Content-Type: application/json; utf-8 
Content-Length: 0 
X-Goog-Channel-ID: 4ba78bf0-6a47-11e2-bcfd-0800200c9a66 
X-Goog-Channel-Token: 398348u3tu83ut8uu38 
X-Goog-Channel-Expiration: Tue, 19 Nov 2013 01:13:52 GMT 
X-Goog-Resource-ID: ret08u3rv24htgh289g 
X-Goog-Resource-URI: https://www.googleapis.com/drive/v2/files/ret08u3rv24htgh289g 
X-Goog-Resource-State: update 
X-Goog-Changed: content,properties 
X-Goog-Message-Number: 10 

改变通知消息用于改变资源,其包括请求体:

POST https://example.com/notifications // Your receiving URL. 
Content-Type: application/json; utf-8 
Content-Length: 118 
X-Goog-Channel-ID: 8bd90be9-3a58-3122-ab43-9823188a5b43 
X-Goog-Channel-Token: 245t1234tt83trrt333 
X-Goog-Channel-Expiration: Tue, 19 Nov 2013 01:13:52 GMT 
X-Goog-Resource-ID: ret987df98743md8g 
X-Goog-Resource-URI: https://www.googleapis.com/drive/v2/changes 
X-Goog-Resource-State: changed 
X-Goog-Message-Number: 23 

{ 
    "kind": "drive#changes", 
    "id": "12345", 
    "selfLink": "https://www.googleapis.com/drive/v2/changes/12345" 
} 

细节请参照https://developers.google.com/drive/web/push#msg-format

+0

我这样做@Marco,我得到请求机构,因为你可以看到,但问题是:我得到了正文,即'{“kind”:“drive#change”,“id”:“8664”,“selfLink”:“https://www.googleapis.com/drive/v2/changes/8664“}'id:8864,当我尝试调用它时说的”404:找不到变化“,所以我减少了1,所以它是8863,然后printChange方法可以找到该文件,如果文件被删除,我需要通过最大的changeId 2来判定哪些文件被删除。我不知道这是正确的方法,但它的工作原理:S – 2015-02-26 08:01:35

+0

请记住,Stack Overflow要求人们提出一个问题。如果问题不断演变,那么请打开一个新问题,并随意链接回到这个问题的上下文。你最初的问题“我如何获得文件ID”是驱动我目前的答案。希望有所帮助。 – Marco 2015-03-02 18:08:52

相关问题