简体   繁体   English

无法使用 REST API 在谷歌驱动器的特定文件夹中上传文件

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

I am trying to upload a local file to a specified folder in Google Drive using REST API from android app, the file is uploading in the root directory but not uploading in the specified folder.我正在尝试使用来自 android 应用程序的 REST API 将本地文件上传到 Google Drive 中的指定文件夹,该文件正在根目录中上传,但未在指定文件夹中上传。 No error in the API. API 中没有错误。

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

Tried passing the folder ID in metadata in following ways:尝试通过以下方式在元数据中传递文件夹 ID:

1. 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());

"parents" parameter isn't working, also tried using "addParents" parameter. “父母”参数不起作用,也尝试使用“addParents”参数。 Kindly suggest a way to specify the parent folder ID.请提出一种指定父文件夹 ID 的方法。

I am not a java or android expert but this may get you started.我不是 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());

or maybe this或者也许这个

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;
    }       
}

pure rest html纯休息html

Im not exactly usre what you mean by "I am using REST API."我并不完全理解您所说的“我正在使用 REST API”。 but if you are trying to do this completely yourself make sure that you build the body properly that is where the metadata is submited.但是,如果您尝试完全自己执行此操作,请确保正确构建正文,这是提交元数据的地方。

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