簡體   English   中英

無法使用 REST API 在谷歌驅動器的特定文件夾中上傳文件

[英]Unable to upload file in specific folder in google drive using REST API

我正在嘗試使用來自 android 應用程序的 REST API 將本地文件上傳到 Google Drive 中的指定文件夾,該文件正在根目錄中上傳,但未在指定文件夾中上傳。 API 中沒有錯誤。

使用以下 API: https ://developers.google.com/drive/api/guides/manage-uploads#multipart

嘗試通過以下方式在元數據中傳遞文件夾 ID:

1.

String[] arr = {parentFolderId};    
jsonObject.put("parents", arr);
jsonObject.put("name", file.getName());
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("id", parentFolderId);
jsonArray.put(jsonObject1);
jsonObject.put("parents", jsonArray);
jsonObject.put("name", file.getName());
ArrayList<String> arrayList = new ArrayList<>(); 
arrayList.add(parentFolderId);
jsonObject.put("parents", arrayList);
jsonObject.put("name", file.getName());

“父母”參數不起作用,也嘗試使用“addParents”參數。 請提出一種指定父文件夾 ID 的方法。

我不是 java 或 android 專家,但這可能會讓你入門。

String folderId = "folderID";
File fileMetadata = new File();
fileMetadata.setName("FileName");
fileMetadata.setParents(Collections.singletonList(folderId));

File file = driveService.files().create(fileMetadata)
.setFields("id, parent")
.execute();
System.out.println("File ID: " + file.getId());

或者也許這個

String UploadFileToGoogleDrive(String sFullPath, String sFileName, String sParentFolderId) {               
    com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
    fileMetadata.setName(sFileName);
    fileMetadata.setParents(Collections.singletonList(sParentFolderId));
    java.io.File filePath = new java.io.File(sFullPath);
    FileContent mediaContent = new FileContent(null, filePath);
    try {
        com.google.api.services.drive.model.File file = googleDriveService.files().create(fileMetadata, mediaContent)
                .setFields("id, parents")
                .execute();
        return file.getId();
    } catch (IOException e) {            
        return null;
    }       
}

純休息html

我並不完全理解您所說的“我正在使用 REST API”。 但是,如果您嘗試完全自己執行此操作,請確保正確構建正文,這是提交元數據的地方。

POST https://www.googleapis.com/drive/v3/files?key=[YOUR_API_KEY] HTTP/1.1

Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
Content-Type: application/json

{
  "name": "test",
  "parents": [
    "folderId"
  ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM