繁体   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