简体   繁体   中英

How to write file in Java with Google Drive API

I'm trying to write a file on my Google Drive.

This is my code:

 /**
 * Update a permission's role.
 *
 * @param service Drive API service instance.
 * @param fileId ID of the file to update permission for.
 * @param permissionId ID of the permission to update.
 * @param newRole The value "owner", "writer" or "reader".
 * @return The updated permission if successful, {@code null} otherwise.
 */
private static Permission updatePermission(Drive service, String fileId,
        String permissionId, String newRole) {
    try {
        // First retrieve the permission from the API.
        Permission permission = service.permissions().get(
                fileId, permissionId).execute();
        permission.setRole(newRole);
        return service.permissions().update(
                fileId, permissionId, permission).execute();
    } catch (IOException e) {
        System.out.println("An error occurred: " + e);
    }
    return null;
}

public static void main(String[] args) throws IOException {

    // Build a new authorized API client service.
    Drive service = getDriveService();

    // File's metadata.
    File file = new File();
    file.setId(??);
    file.setTitle("Title");
    file.setDescription("Descrizione");
    file.setMimeType("application/vnd.google-apps.drive-sdk");

    updatePermission(service, file.getId(), ??, "writer");

    file = service.files().insert(file).execute();
}

Like you can see, in the Main I'm setting the file properties. But I don't know how do it.

1) What is file id ? In file.setId(""); if I set, for example, "1" gradle tell me:

An error occurred: com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
  "code" : 404,
  "errors" : [ {
  "domain" : "global",
  "location" : "file",
  "locationType" : "other",
  "message" : "File not found: 1",
  "reason" : "notFound"
  } ],
  "message" : "File not found: 1"
}
Exception in thread "main"      com.google.api.client.googleapis.json.GoogleJsonResponseException: 403      Forbidden
{
    "code" : 403,
    "errors" : [ {
    "domain" : "global",
    "message" : "Insufficient Permission",
    "reason" : "insufficientPermissions"
    } ],
    "message" : "Insufficient Permission"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at DriveQuickStart.main(DriveQuickStart.java:152)
:run FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/usr/local/jdk1.7.0_80/bin/java'' finished with   non-zero exit value 1

So, what is file id and how can I generate it?

2) In updatePermission what is the id to set?

3) I want to upload a file that I have on my desktop, how can I set the path to this file?

Thanks.

The best approach would be to look at Drive REST API reference , especially the ' TryIt ' playgroud on each of the reference pages.

For instance, start with ' insert ', create a file using TryIt , and you will see the ID of the file you created. It is a funky-looking string sometimes called ' ResourceId ' in the Android universe (you will also see it in the 'get shareable link' address).
You can also get this ID by using queries (by title, mimetype, modification date, ...) as seen in ' TryIt ' of the 'list' here .

Uploading file from 'your desktop' involves what you see in the ' insert ' reference section, ie you set

  1. metadata describing the file (title, mimetype, flags,...)
  2. file content on the form of java.io.File,

call:

File file = service.files().insert(metdata, content).execute();

and you get your ID... but I'm repeating myself.

So, the short answer is:
In you main(), you get the ID from either some kind of search ( list ) or you have it around from previously created file ( insert ).
You can even copy/paste it from the 'shareable link' http address you snatched from the https://drive.google.com .

Good Luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM