简体   繁体   English

如何使用Google Drive API用Java编写文件

[英]How to write file in Java with Google Drive API

I'm trying to write a file on my Google Drive. 我正在尝试在Google云端硬盘上写入文件。

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. 如您所见,在Main中,我正在设置文件属性。 But I don't know how do it. 但是我不知道怎么做。

1) What is file id ? 1)什么是文件ID In file.setId(""); file.setId(""); if I set, for example, "1" gradle tell me: 例如,如果我设置了“ 1” gradle,请告诉我:

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? 那么,什么是文件ID?如何生成它?

2) In updatePermission what is the id to set? 2)在updatePermission中,要设置的ID是什么?

3) I want to upload a file that I have on my desktop, how can I set the path to this file? 3)我要上传桌面上的文件,如何设置该文件的路径?

Thanks. 谢谢。

The best approach would be to look at Drive REST API reference , especially the ' TryIt ' playgroud on each of the reference pages. 最好的方法是查看Drive REST API参考 ,尤其是每个参考页面上的“ TryIt ”方法。

For instance, start with ' insert ', create a file using TryIt , and you will see the ID of the file you created. 例如,以' insert '开头,使用TryIt创建文件,您将看到创建的文件的ID。 It is a funky-looking string sometimes called ' ResourceId ' in the Android universe (you will also see it in the 'get shareable link' address). 这是一个看起来很时髦的字符串,在Android Universe中有时也称为“ ResourceId ”(您还将在“获取可共享链接”地址中看到它)。
You can also get this ID by using queries (by title, mimetype, modification date, ...) as seen in ' TryIt ' of the 'list' here . 您还可以通过使用查询(按标题,mimetype,修改日期等)来获取此ID,如此处“列表”的TryIt ”所示。

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, 以java.io.File形式的文件内容,

call: 呼叫:

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

and you get your ID... but I'm repeating myself. 您会得到您的ID ...但是我在重复我自己。

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 ). 在main()中,您可以通过某种搜索( 列表 )获取ID ,也可以从先前创建的文件( 插入 )中获取ID
You can even copy/paste it from the 'shareable link' http address you snatched from the https://drive.google.com . 您甚至可以从您从https://drive.google.com抢夺的“共享链接” http地址复制/粘贴它。

Good Luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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