我试图在Google云端硬盘上写一个文件。
这是我的代码:
/**
* 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();
}
就像你可以看到的那样,在Main I中设置文件属性。但我不知道怎么做。
1)什么是文件ID ?例如,file.setId("");
如果我设置了" 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
那么,什么是文件ID以及如何生成它?
2)在updatePermission中,要设置的id是什么?
3)我想上传我桌面上的文件,如何设置此文件的路径?
感谢。
答案 0 :(得分:0)
最好的方法是在每个参考页面上查看云端硬盘REST API reference,尤其是' TryIt 'playgroud。
例如,以“insert”开头,使用 TryIt 创建文件,您将看到您创建的文件的ID。这是一个看似时髦的字符串,有时在Android世界中称为“ResourceId”(您也会在“获取可共享链接”地址中看到它。) 您还可以使用'list' here的' TryIt '中的查询(按标题,mimetype,修改日期等...)获取此ID。
从“您的桌面”上传文件涉及您在“insert”参考部分中看到的内容,即您设置
致电:
文件file = service.files()。insert(metdata,content).execute();
你得到了你的身份证......但我正在重复自己。
所以,简短的回答是:
在main()中,您可以从某种搜索(list)获取 ID ,或者从之前创建的文件(insert)获取 ID 。
您甚至可以从 https://drive.google.com 中抓取的“可共享链接”http地址中复制/粘贴它。
祝你好运