英文:
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&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 = 'https://www.googleapis.com/upload/drive/v3/files?
uploadType=multipart&parents=' + 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('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) {
// File uploaded successfully
System.debug('File uploaded successfully to Google Drive');
} else {
// Error uploading file
System.debug('Error uploading file to Google Drive: ' + response.getStatus() + ' - ' + 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&parents=%5C<folder ID>
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: \<folder ID\>
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&parents=%5C<folder ID>
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:{"parents":["folderid"]}
Look here for metadata options and correct JSON representation: Google Drive API Files Overview.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论