0

在我的 Java 项目中,我使用 Google Drive API 来从公共驱动器下载文件。

当我尝试根据某些标准列出某个文件夹的子项时,一切都按预期运行。然而,有这个modifiedDate字段不返回任何结果。没有出现错误,但列表是空的,尽管编写了一个完全合理的搜索子句(应该有相当多的文件)。

这是我的代码:

    Drive.Children.List firstRequest = service.children().list(folderId)
            .setQ("mimeType='application/pdf' and modifiedDate <= '2016-03-07T19:17:54.759Z'" );
    do {
        ChildList result = firstRequest.execute();
        for(ChildReference child: result.getItems()) {
            File file = service.files().get(child.getId()).execute();
            System.out.printf("Found file: %s (%s): %s\n",
                    file.getTitle(), file.getId(), file.getCreatedDate());
        }
        firstRequest.setPageToken(result.getNextPageToken());
    } while (firstRequest.getPageToken() != null &&
            firstRequest.getPageToken().length() > 0);

使用试一试!Google Developers 页面上的功能相同的查询会按预期返回结果。那里的HTTP请求如下:

在此处输入图像描述

这是回应: 在此处输入图像描述

当我运行我的 Java 应用程序时,这是请求 URL:

GET  https://www.googleapis.com/drive/v2/files/0B3Y7hLeztp6SUVBCbnpRN0ZQVVU/children?q=mimeType%3D'application/pdf'%20and%20modifiedDate%20%3C%3D%20'2016-03-07T19:17:54.759Z'

HTTP 响应如下:

kind: drive#childList
etag: "TEeH8_qJkyJwjzQ-F4FJRbfwWxI/vyGp6PvFo4RvsFtPoIWeCReyIC8"
selfLink: https://www.googleapis.com/drive/v2/files/0B3Y7hLeztp6SUVBCbnpRN0ZQVVU/children?q=mimeType%3D'application/pdf'+and+modifiedDate+%3C%3D+'2016-03-07T19:17:54.759Z'
items: [


]

请求是相同的。但反应不是......可能是什么原因?

编辑

我不得不提到我尝试使用服务帐户。这将是一个桌面应用程序,因此我不一定希望用户承认使用 Google API 并被迫创建一个 Gmail 地址以防万一他们没有一个并授权请求。此外,该应用程序仅访问公开共享的驱动器上的资源,不需要有关客户端的信息。

但是,服务帐户密钥导致了我当前的问题,而使用 OAuth 客户端 ID 密钥可以让我获得所需的结果。

处理这种情况的推荐方法是什么?或者,也许我对 OAuth2 框架的一切都搞错了?

4

0 回答 0