Search code examples
javagoogle-apigoogle-drive-apigoogle-api-java-client

Valid values for setFields in Google Drive API


I've taken the following code from 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();
    }
}

The .setFields("*") causes the following Bad request response.

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"

If I change the * in setfields to text then I get invalid parameter. If I remove it altogether I get no errors. I've googled to try to determine what the possible parameters are for setFields in this case but I haven't found anything.

Where do I find the list of possible parameters for setFields in this instance?

Why does the above code fail when setFields is set to *

I'm using the following dependency

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

Regards Conteh


Solution

  • The setField for Drive API is used for partial responses, it will depend on what data you want that will be part of the returned object.

    Setting "*" on it won't work since it doesn't represent any field in the Response object. For it to work, you either do not set the fields to get all values, or specify fields that are only necessary (depends on the API you're calling, for changeList, it can be changes, nextPageToken, newStartPageToken, kind