Reputation: 1614
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
Upvotes: 3
Views: 6053
Reputation: 894
As @adjuremods pointed out, you can retrieve all the fields by skipping 'setFields', that is, setting no specific field. Adding to his answer and answering your question on the fourth paragraph (hopefully not too late to help other developers): "Where do I find the list of possible parameters for setFields in this instance?"
You can choose the fields to set on the request from this list of properties: https://developers.google.com/drive/api/v3/reference/files#properties Just be careful to check if the property is available. For example, 'imageMediaMetadata' is available only for image files; 'thumbnailLink' is available when you request for 'metadata' and not available for request 'create'; and so on.
Upvotes: 1
Reputation: 1440
Removing .setFilters works but I still wanted to lighten traffic and memory usage. This list helped me to find the field name for mime type, which turned out to be 'mimeType' case sensitive! try fields listed here
I needed to sort out folders from all the files, cause folders are files too on google drive. Here is all what I needed:
.setFields("nextPageToken, files(id, name, mimeType)")
Good luck.
Upvotes: 1
Reputation: 1614
The code in the question that I asked needed to be separated into two functions as follows as the SAVED_START_PAGE_TOKEN needs to be first set and then any subsequent changes made to the drive can be listed. I'm posting this so it's clear.
/**
* 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();
}
}
Upvotes: 0
Reputation: 2998
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
Upvotes: 3