【问题标题】:How to get ID of changed file on Google Drive如何获取 Google Drive 上已更改文件的 ID
【发布时间】:2015-02-24 13:42:26
【问题描述】:

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

  1. 在开发控制台、域等上注册 Web 应用程序...
  2. OAuth 授权
  3. 如果不存在则创建频道
  4. 设置 servlet 在发生更改时等待 google drive POST 请求
  5. Google 云端硬盘发生更改(上传或删除)
  6. Servlet 在更改发生时从 Google Drive 捕获 POST 请求
  7. 缓存消息:{ "kind": "drive#change", "id": "8664", "selfLink": "https://www.googleapis.com/drive/v2/changes/8664"}

文件更改的 Google Drive API 表示:

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

如何获取fileId?为什么 Google Drive 不向我发送缺少的字段?我做错了什么?

手表换码:

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;
    }

...很明显,谷歌驱动器发送的应用程序捕获更改,我被卡住了请帮助!

【问题讨论】:

  • 您在寻找什么样的缺失参数?请查看此页面以获取响应资源:developers.google.com/drive/web/push
  • 谢谢我读了它,所以这个推送通知并没有真正告诉我什么文件被改变了,只通知我谷歌驱动器上的东西已经改变了?然后我尝试使用“Changes:get”API,参数:(Drive service,String changeId)我通过{“kind”:“drive#change”,“id”:“8664”, "selfLink": "googleapis.com/drive/v2/changes/8664"} id 像 String changedId 但我总是得到 "Change not found: 8754" 用于上传或删除

标签: java google-drive-api


【解决方案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

【讨论】:

  • 我正在这样做@Marco,如您所见,我得到了请求正文,但问题是:我得到了正文,即{ "kind": "drive#change", "id": "8664", "selfLink": "https://www.googleapis.com/drive/v2/changes/8664"},ID:8864,当我尝试调用该更改时,它说“ 404:未找到更改”所以我将该数字减少了 1,因此它是 8863,然后方法 printChange 可以找到该文件,如果文件被删除,我需要通过 2 的最大 changeId 来确定删除的文件。我不知道这是否是正确的方法,但它有效:S
  • 请记住,Stack Overflow 要求人们提出一个问题。如果问题发生了变化,请打开一个新问题并随时链接回该问题以获取上下文。您最初的问题“我如何获取文件 ID”是推动我当前答案的原因。希望对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2021-12-21
  • 2015-01-22
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多