Google 驱动器 API 中 setFields 的有效值

Valid values for setFields in Google Drive API

我从 google documentation.

中获取了以下代码
public static void detectDriveChanges() throws IOException {

    StartPageToken response = DRIVE.changes()
            .getStartPageToken().execute();

    String savedStartPageToken = response.getStartPageToken();
    System.out.println("Start token: " + savedStartPageToken);

    // Begin with our last saved start token for this user or the
    // current token from getStartPageToken()
    String pageToken = savedStartPageToken;
    while (pageToken != null) {
        ChangeList changes = DRIVE.changes().list(pageToken)
                .setFields("*")
                .execute();
        for (Change change : changes.getChanges()) {
            // Process change
            System.out.println("Change found for file: " + change.getFileId());
        }
        if (changes.getNewStartPageToken() != null) {
            // Last page, save this token for the next polling interval
            savedStartPageToken = changes.getNewStartPageToken();


        }
        pageToken = changes.getNextPageToken();
    }
}

.setFields("*") 导致以下错误请求响应。

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Bad Request",
    "reason" : "badRequest"
  } ],
  "message" : "Bad Request"

如果我将 setfields 中的 * 更改为文本,则会得到无效参数。如果我完全删除它,我不会出错。我已经 googled 尝试确定在这种情况下 setFields 的可能参数是什么,但我没有找到任何东西。

在此实例中,我在哪里可以找到 setFields 的可能参数列表?

为什么setFields设置为*

时上面的代码会失败

我正在使用以下依赖项

<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-drive</artifactId>
    <version>v3-rev40-1.22.0</version>
</dependency>

问候 康特

Drive API 的 setField 用于部分响应,这取决于您想要哪些数据将成为返回对象的一部分。

在其上设置“*”将不起作用,因为它不代表 Response 对象中的任何字段。为了让它起作用,您要么不设置字段以获取所有值,要么指定只需要的字段(取决于您调用的 API,对于 changeList,它可以是 changesnextPageTokennewStartPageTokenkind

我问的问题中的代码需要分为以下两个函数,因为 SAVED_START_PAGE_TOKEN 需要首先设置,然后可以列出对驱动器所做的任何后续更改。我发布这个所以很清楚。

/**
 * Sets SAVED_START_PAGE_TOKEN. Now any changes in google drive 
 * the occur after this point can be listed in the the function
 * detectDriveChanges
 * @throws IOException 
 */
public static void SetStartPageToken() throws IOException {
    StartPageToken response = DRIVE.changes().getStartPageToken().execute();
    SAVED_START_PAGE_TOKEN = response.getStartPageToken();
    System.out.println("Start token: " + SAVED_START_PAGE_TOKEN);
}

/**
 * List any changes to the google drive since the last time
 * SAVED_START_PAGE_TOKEN was set
 * @throws IOException
 */
public static void detectDriveChanges() throws IOException {
    // Begin with our last saved start token for this user or the
    // current token from getStartPageToken()
    String pageToken = SAVED_START_PAGE_TOKEN;
    while (pageToken != null) {
        ChangeList changes = DRIVE.changes().list(pageToken)
                .setFields("changes")
                .execute();
        for (Change change : changes.getChanges()) {
            System.out.println("Change found for file: " + change.getFileId());
        }
        if (changes.getNewStartPageToken() != null) {
            // Last page, save this token for the next polling interval
            SAVED_START_PAGE_TOKEN = changes.getNewStartPageToken();
        }
        pageToken = changes.getNextPageToken();
    }
}

删除 .setFilters 有效,但我仍然想减轻流量和内存使用量。 这个列表帮助我找到了 mime 类型的字段名称,结果是 'mimeType' 区分大小写! try fields listed here

我需要从所有文件中整理出文件夹,因为文件夹也是 google 驱动器上的文件。 这就是我所需要的:

.setFields("nextPageToken, files(id, name, mimeType)")

祝你好运。

正如@adjuremods 所指出的,您可以通过跳过 'setFields' 来检索所有字段,即不设置特定字段。 添加到他的答案并回答您关于第四段的问题(希望帮助其他开发人员不会太晚):“在这种情况下,我在哪里可以找到 setFields 的可能参数列表?

您可以从此属性列表中选择要在请求中设置的字段:https://developers.google.com/drive/api/v3/reference/files#properties 请注意检查 属性 是否可用。例如,'imageMediaMetadata' 仅适用于图像文件; 'thumbnailLink' 在您请求 'metadata' 时可用,而在请求 'create' 时不可用;等等。