[英]How to write file in Java with Google Drive API
我正在嘗試在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中,我正在設置文件屬性。 但是我不知道怎么做。
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)我要上傳桌面上的文件,如何設置該文件的路徑?
謝謝。
最好的方法是查看Drive REST API參考 ,尤其是每個參考頁面上的“ TryIt ”方法。
例如,以' insert '開頭,使用TryIt創建文件,您將看到創建的文件的ID。 這是一個看起來很時髦的字符串,在Android Universe中有時也稱為“ ResourceId ”(您還將在“獲取可共享鏈接”地址中看到它)。
您還可以通過使用查詢(按標題,mimetype,修改日期等)來獲取此ID,如此處“列表”的 “ TryIt ”所示。
從“您的桌面”上載文件涉及您在“ 插入 ”參考部分中看到的內容,即您進行了設置
呼叫:
文件file = service.files()。insert(metdata,content).execute();
您會得到您的ID ...但是我在重復我自己。
因此,簡短的答案是:
在main()中,您可以通過某種搜索( 列表 )獲取ID ,也可以從先前創建的文件( 插入 )中獲取ID 。
您甚至可以從您從https://drive.google.com搶奪的“共享鏈接” http地址復制/粘貼它。
祝好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.