Google api 使用googledriveapi的Http多部分请求
我正在尝试使用GoogleDrive API发送HTTP多部分请求,以便在GoogleDrive中插入文件 我正在关注以下链接: 但是,我收到了一个Google api 使用googledriveapi的Http多部分请求,google-api,http-post,google-drive-api,http-put,Google Api,Http Post,Google Drive Api,Http Put,我正在尝试使用GoogleDrive API发送HTTP多部分请求,以便在GoogleDrive中插入文件 我正在关注以下链接: 但是,我收到了一个错误请求 下面是我使用上面的文档链接创建的请求字符串: String content = '\r\n--' + boundary + '\r\n'; content +='Content-Type: '+'application/json; charset=UTF-8'+'\r\n\r\n'; content +='{' +
错误请求
下面是我使用上面的文档链接创建的请求字符串:
String content = '\r\n--' + boundary + '\r\n';
content +='Content-Type: '+'application/json; charset=UTF-8'+'\r\n\r\n';
content +='{' + '\r\n';
content +='"title": "My File"'+'\r\n';
content +='}'+'\r\n\r\n';
content += '--'+boundary + '\r\n';
content +='Content-Type: '+'text/plain'+'\r\n';
content += EncodingUtil.base64Encode(b)+'\r\n';
content += '-'+boundary+'-\r\n';
有人能告诉我这里缺少了什么吗?我也有这个问题,在尝试了一些更改后,我终于找到了一个工作示例: 标题:
POST /upload/drive/v2/files?uploadType=multipart&access_token=ya29.CjAmA2j6eonCiROaNum-V1cWdFVH2vXpNiXAsXK6iLPu7K54tD4uNsmH-eEycMcnaBE HTTP/1.1
Host: www.googleapis.com
Accept: */*
Content-Type: multipart/related; boundary="foo_bar_baz"
Content-Length: 150
--foo_bar_baz
Content-Type: application/json; charset=UTF-8
{
"title": "My File"
}
--foo_bar_baz
Content-Type: text/txt
JPEG data
--foo_bar_baz--
记住在内容类型字段中添加boundary=“foo\u bar\u baz”
正文:
POST /upload/drive/v2/files?uploadType=multipart&access_token=ya29.CjAmA2j6eonCiROaNum-V1cWdFVH2vXpNiXAsXK6iLPu7K54tD4uNsmH-eEycMcnaBE HTTP/1.1
Host: www.googleapis.com
Accept: */*
Content-Type: multipart/related; boundary="foo_bar_baz"
Content-Length: 150
--foo_bar_baz
Content-Type: application/json; charset=UTF-8
{
"title": "My File"
}
--foo_bar_baz
Content-Type: text/txt
JPEG data
--foo_bar_baz--
我在这方面也遇到了麻烦, 但如果您查看github上google drive API的代码: 请求参数接受一个媒体对象,该对象可以有body和mimeType 我使用的是一个服务帐户,这可以让你直接上传文件到硬盘
auth.getApplicationDefault(function(err, authClient) {
if (err) {
console.log('Authentication failed because of ', err);
return;
}
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
var scopes = ['https://www.googleapis.com/auth/drive'];
authClient = authClient.createScoped(scopes);
}
var request = {
project: "YOUR_PROJECT",
auth: authClient,
resource: {
parents: ['blah']
},
media: {
body: 'hi',
mimeType: 'text/plain'
}
};
drive.files.create(request, function(err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
}
});
});
您手动执行此操作而不使用客户端库的?