如何在Google云端硬盘上获取已更改文件的ID

时间:2015-02-24 13:42:27

标签: java google-drive-api

我在我的网站上集成了google drive API推送通知。 目前的工作:

  1. 在开发者控制台,域等上注册网络应用程序......
  2. OAuth授权
  3. 如果不存在则创建频道
  4. 如果发生更改,则等待谷歌驱动器POST请求的安装servlet
  5. 谷歌驱动器上传(上传或删除)
  6. Servlet会在发生更改时从Google驱动器捕获POST请求
  7. 缓存消息:{ "kind": "drive#change", "id": "8664", "selfLink": "https://www.googleapis.com/drive/v2/changes/8664"}
  8. 对文件进行更改的Google驱动器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;
        }
    

    ...显然app抓住了谷歌驱动器发送的变化,我被困住了PLZ帮助!

1 个答案:

答案 0 :(得分: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