Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive API v3 Migration

I decided to migrate from Google Drive API v2 to v3 and it was not an easy task. Even thought Google wrote this documentation, there are many gaps in it and there is not much information about this on the web.

I'm sharing here what I have found.

like image 719
IvanRF Avatar asked Feb 02 '16 01:02

IvanRF


People also ask

What is API V2 and V3?

V2 and V3 Catalog REST APIs allow you to manage your store's products, categories, and brands, along with their sub-resources. Both the V2 and V3 Catalog APIs authenticate with OAuth and can be used concurrently within a single application; however, data representation can be significantly different.

What is V3 API?

About the V3 API The new V3 API provides fast, easy access to MBTA schedules, alerts, and real-time information. The V3 API uses the JSON API format, so you can get started quickly using any of the available libraries. V3 API documentation is available using Swagger.

How do I migrate Google Drive?

Drive and Docs. Click Transfer ownership. For From user, enter the current owner's email address and select the user from the results. For To user, enter the new owner's email address and select the user from the results. Click Transfer Files.


2 Answers

First, read the official docs: v2 to v3 reference | Drive API v3 versus v2 | Migrate to v3


Download

Download has changed. The field downloadUrl does not exist anymore. Now, it can be achieved with this:

service.files().get(fileId).executeMediaAndDownloadTo(outputStream);

I tried the new field webContentLink but it returns HTML content, instead of the file's content. In other words, it gives you the link to the Drive web UI.


Upload

Upload only requires to change the word insert for create, nothing more.


Trash / Update

I wasted some time with this one 😔. Used to be a simple service.files().trash(fileId).execute(). Docs say

files.trash -> files.update with {'trashed':true}

The example code for update on v2 makes a get on the file, sets new values and then calls update.

On v3, using update like that throws this exception:

{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "The resource body includes fields which are not directly writable.",
    "reason" : "fieldNotWritable"
  } ],
  "message" : "The resource body includes fields which are not directly writable."
}

The solution is to create an empty File setting only the new values:

File newContent = new File();
newContent.setTrashed(true);
service.files().update(fileId, newContent).execute();

Note: File refers to com.google.api.services.drive.model.File (it is not java.io.File).


List

Files returned by service.files().list() does not contain information now, i.e. every field is null. If you want list on v3 to behave like in v2, call it like this:

service.files().list().setFields("nextPageToken, files");

Docs on Search for files and folders use setFields("nextPageToken, files(id, name)") but there is no documentation on how to get all the info for a file. Now you know, just include "files".


Fields

Full resources are no longer returned by default. Use the fields query parameter to request specific fields to be returned. If left unspecified only a subset of commonly used fields are returned.

That last part is not totally true, since you are forced to use setFields in some cases. For example, if you use service.about().get().execute() you will get this error:

"The 'fields' parameter is required for this method."

which is solved by calling service.about().get().setFields("user, storageQuota").execute(), for instance.

At the end of the docs is mentioned as:

The fields query parameter should be specified for methods which return


For the rest of the changes, just follow the Google table on the docs.

like image 149
IvanRF Avatar answered Oct 02 '22 00:10

IvanRF


I don't have the reputation to comment (sorry), but the provided line of code in the accepted answer, service.about().get().setFields("user, storageQuota").execute(), does not run, instead it throws an attribute error:

    about = service.about().get().setFields("user", "storageQuota").execute()
AttributeError: 'HttpRequest' object has no attribute 'setFields'

Instead, the line should read:

service.about().get(fields = "user, storageQuota").execute()
like image 30
Meg Avatar answered Oct 02 '22 01:10

Meg