将文件上传到Google Drive API中的特定文件夹

huangapple go评论199阅读模式
英文:

Uploading a File to a Specific Folder in Google Drive API

问题

我尝试将文件上传到Google Drive API中的特定文件夹,但我在使其工作方面遇到了问题。我尝试过使用Apex中的代码和使用Postman进行HTTP请求,但文件始终最终出现在根文件夹中。

这是我在Apex中使用的代码来进行HTTP请求:

public void uploadFileToDrive(String folderId) {
   // 获取HTTP请求正文
   String requestBody = createRequestBody();
   String strEndpoint = 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&parents=' + folderId;

    // 发起HTTP POST请求以将文件上传到指定的文件夹
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint(strEndpoint);
    request.setHeader('Authorization', 'Bearer ' + access_token);
    request.setHeader('Content-Type', 'multipart/related; boundary="----boundary"');
    request.setBody(requestBody);
    request.setMethod('POST');
    HttpResponse response = http.send(request);
    if (response.getStatusCode() == 200) {
        // 文件成功上传
        System.debug('文件成功上传到Google Drive');
    } else {
        // 上传文件时出错
        System.debug('将文件上传到Google Drive时出错:' + response.getStatus() + ' - ' + response.getBody());
    }
}

我尝试将文件夹ID传递给URL中的parents参数,如下所示:

https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&parents=

但这没有起作用。文件继续上传到根文件夹。

我还尝试在Postman中测试端点,但结果相同。这是我在form-data中发送的内容:

Key: file, Value: test.png
Key: parents, Value: <folder ID>

我将Content-Type标头设置为multipart/form-data。

有人能帮助我弄清楚我做错了什么吗?提前谢谢!

我尝试将文件上传到Google Drive API中的特定文件夹,但我在使其工作方面遇到了问题。我尝试将文件夹ID传递给URL中的parents参数,如下所示:

https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&amp;parents=

我期望文件将上传到指定的文件夹,但它一直被上传到根文件夹。我还尝试在Postman中测试端点,但结果相同。

有人能帮助我弄清楚我做错了什么吗?提前谢谢!

英文:

I'm trying to upload a file to a specific folder in Google Drive API, but I'm having trouble getting it to work. I've tried both through code in Apex and using Postman to make HTTP requests, but the file always ends up in the root folder.

Here's the Apex code I'm using to make the HTTP request:

public void uploadFileToDrive(String folderId) {
   // Get the HTTP request body
   String requestBody = createRequestBody();
   String strEndpoint =  &#39;https://www.googleapis.com/upload/drive/v3/files? 
   uploadType=multipart&amp;parents=&#39; + folderId;

    // Make the HTTP POST request to upload the file to the specified folder
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint(strEndpoint);
    request.setHeader(&#39;Authorization&#39;, &#39;Bearer &#39; + access_token);
    request.setHeader(&#39;Content-Type&#39;, &#39;multipart/related; boundary=&quot;----boundary&quot;&#39;);
    request.setBody(requestBody);
    request.setMethod(&#39;POST&#39;);
    HttpResponse response = http.send(request);
    if (response.getStatusCode() == 200) {
        // File uploaded successfully
        System.debug(&#39;File uploaded successfully to Google Drive&#39;);
    } else {
        // Error uploading file
        System.debug(&#39;Error uploading file to Google Drive: &#39; + response.getStatus() + &#39; - &#39; + response.getBody());
    }

}

I've tried passing the folder ID to the parents parameter in the URL like this:

https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&amp;parents=%5C&lt;folder ID&gt;

But it's not working. The file keeps getting uploaded to the root folder.

I've also tried testing the endpoint in Postman, but I'm getting the same result. Here's what I'm sending in the form-data:

Key: file, Value: test.png
Key: parents, Value: \&lt;folder ID\&gt;

I'm setting the Content-Type header to multipart/form-data.

Can anyone help me figure out what I'm doing wrong? Thank you in advance!

I'm trying to upload a file to a specific folder in Google Drive API, but I'm having trouble getting it to work. I've tried both through code in Apex and using Postman to make HTTP requests, but the file always ends up in the root folder.

I tried passing the folder ID to the parents parameter in the URL like this:

https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&amp;parents=%5C&lt;folder ID&gt;

I expected the file to be uploaded to the specified folder, but it keeps getting uploaded to the root folder. I also tried testing the endpoint in Postman, but I'm getting the same result.

Can anyone help me figure out what I'm doing wrong? Thank you in advance!

答案1

得分: 0

文件夹的ID必须添加到表单数据中,而不是URL。但关键不是parents,而是metadata

关键:metadata,值:{"parents":["folderid"]}

有关元数据选项和正确的JSON表示,请查看这里:Google Drive API文件概述

英文:

The folder id must be added to the form data, not to the URL. But the key is not parents, but metadata.

Key:metadata, value:{&quot;parents&quot;:[&quot;folderid&quot;]}

Look here for metadata options and correct JSON representation: Google Drive API Files Overview.

huangapple
  • 本文由 发表于 2023年3月8日 14:26:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75669963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定