0

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.

Dave
  • 1,428
  • 4
  • 31
  • 49

1 Answers1

0

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, i.e. 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

Community
  • 1
  • 1
seanpj
  • 6,735
  • 2
  • 33
  • 54
  • I'm using Linux os. Ok, I'm trying to insert file with private `static File insertFile(Drive service, String title, String description, String parentId, String mimeType, String filename)` but **parentId** what is? I don't understand.. – Dave Jul 19 '15 at 13:03
  • Firstly, each file (or folder) in Google Drive has a unique ID. If you didn't know that, then you haven't done your homework. You need to go back and read the docs. Having done that, you'll quickly realise that parentId is the fileId of the folder into which you want to insert the new file. – pinoyyid Jul 19 '15 at 17:24
  • Google Drive's file system is flat, but a tree structure can be faked by existence of parent links - parent IDs. But be careful, each object (file, folder) can have multiple parents, so a traditional tree file/folder structure can be created, but is not enforced. – seanpj Jul 19 '15 at 21:25