java-samples icon indicating copy to clipboard operation
java-samples copied to clipboard

SearchFile.java sample code needs to be updated for API v3?

Open RobustCoder opened this issue 6 months ago • 0 comments

I tried running the Java sample code in the file SearchFile.java on the official doc page, which is also stored here.

That code fails for me with this error output:

GET https://www.googleapis.com/drive/v3/files?fields=nextPageToken,%20items(id,%20title)&spaces=drive
{
  "code": 400,
  "errors": [
    {
      "domain": "global",
      "location": "fields",
      "locationType": "parameter",
      "message": "Invalid field selection items",
      "reason": "invalidParameter"
    }
  ],
  "message": "Invalid field selection items"
}
	at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
	at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:118)
	at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:37)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$3.interceptResponse(AbstractGoogleClientRequest.java:479)
	at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1111)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:565)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:506)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:616)

The key part of that error message is Invalid field selection items

It took me a while to realize that this line of code in SearchFile.java is the offender: .setFields("nextPageToken, items(id, title)")

I think that "items" is a API V2 term, whereas I am using the modern API V3, in which case that line needs to be changed to something like .setFields("nextPageToken, files(name, id)")

I got that idea from this link.

All the google sample code ought to work with API V3 at this point, right?

RobustCoder avatar Jul 09 '25 04:07 RobustCoder