Google Drive API中setFields的有效值

时间:2016-09-07 11:15:44

标签: java google-api google-drive-api google-api-java-client

我从谷歌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中的*更改为text,则会得到无效参数。如果我完全删除它我没有错误。在这种情况下,我用谷歌搜索来确定setFields的可能参数是什么,但我还没有找到任何东西。

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

当setFields设置为*

时,为什么上述代码失败

我使用以下依赖

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

此致 康特

4 个答案:

答案 0 :(得分:2)

Drive API的setField用于部分响应,它取决于您希望哪些数据成为返回对象的一部分。

设置&#34; *&#34;它不会起作用,因为它不代表Response对象中的任何字段。要使其工作,您要么不设置字段以获取所有值,要么指定仅需要的字段(取决于您要调用的API,对于changeList,它可以是changes,{{ 1}},nextPageTokennewStartPageToken

答案 1 :(得分:0)

我要求的问题中的代码需要分成两个函数,因为需要先设置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();
    }
}

答案 2 :(得分:0)

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

我需要整理所有文件中的文件夹,因为文件夹也是google驱动器上的文件。 以下是我所需要的一切:

.setFields(“nextPageToken,files(id,name,mimeType)”)

祝你好运。

答案 3 :(得分:0)

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

您可以从以下属性列表中选择要在请求上设置的字段:https://developers.google.com/drive/api/v3/reference/files#properties 请小心检查该属性是否可用。例如,“ imageMediaMetadata”仅适用于图像文件。当请求“元数据”时,“ thumbnailLink”可用,而对于“创建”请求,则不可用;等等。