Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google drive api 将图像发布到Google Drive API V3时出现问题_Google Drive Api - Fatal编程技术网

Google drive api 将图像发布到Google Drive API V3时出现问题

Google drive api 将图像发布到Google Drive API V3时出现问题,google-drive-api,Google Drive Api,我在向发布图像时遇到问题。该文件是在我的谷歌硬盘上创建的,但是jpeg的大小始终为35KB,因此似乎没有上传任何图像内容 起初我认为这是一个bug,但现在我怀疑它与GoogleDriveAPIv3有关。当我尝试将图像直接发布到API时,我遇到了同样的问题 请求 POST https://www.googleapis.com/upload/drive/v3/files?uploadType=media Accept: */* Accept-Encoding: gzip, deflate Conte

我在向发布图像时遇到问题。该文件是在我的谷歌硬盘上创建的,但是jpeg的大小始终为35KB,因此似乎没有上传任何图像内容

起初我认为这是一个bug,但现在我怀疑它与GoogleDriveAPIv3有关。当我尝试将图像直接发布到API时,我遇到了同样的问题

请求

POST https://www.googleapis.com/upload/drive/v3/files?uploadType=media
Accept: */*
Accept-Encoding: gzip, deflate
Content-Type: image/jpeg
Content-Length: 26837
Accept-Language: en-ca
Authorization: Bearer ya29.Gl1pB5BN9khs33ygA9pnIbyxJYp87teqDUfm55EsEHmmPJz0...

/9j/4AAQSkZJRgABAQEASABIAAD//gAMQXBwbGVNYXJrCv/bAIQABwUFBgUFBwYGBggHBwgKEQs...
响应

HTTP/1.1 200 OK

Server: UploadServer
Content-Type: application/json; charset=UTF-8
X-GUploader-UploadID: AEnB2UrjBAKhEMI-rCdzXpfAF3rM3oBABNUVH9AnhFtoXsoRroYV3By4rVqEuU1mn_5rJ2u_msN99Z1m2r-6Lh53fAowxmQ7QGAgWr2bP6cEwfnfV9JE-Vc
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Vary: Origin, X-Origin
Date: Mon, 19 Aug 2019 17:04:18 GMT
Alt-Svc: quic=":443"; ma=2592000; v="46,43,39"
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Length: 118

是否有人能够成功地将图像上载到?知道哪里出了问题吗?

这次修改怎么样?关于
uploadType=media
,无法包含元数据,并且我找不到上载和转换base64数据的方法。当我看到你的问题时,我注意到你尝试了两种方法。一种是
uploadType=media
。另一个是
uploadType=multipart
。在链接中,我还确认两个脚本都不起作用

uploadType=multipart
可以上载base64数据并将其转换为二进制数据。因此,在这里,我想建议对
uploadType=multipart
进行修改

修改点: 我认为你的要求几乎是正确的。但它需要稍加修改

  • 在您的问题中,您说过数据已转换为base64。在这种情况下,需要将内容传输编码设置为请求正文
  • 将文件上载到Google Drive时,即使未设置
    内容长度
    ,也可以上载文件
修改脚本: 请修改
contentDisposition()
,如下所示

发件人: 致: 并且,请修改
uploadGoogleMultipart()
,如下所示

发件人: 致: 注:
  • 在这次修改中,它假设您已经能够使用driveapi将文件上传到googledrive
参考:

如果我误解了你的问题,而这不是你想要的方向,我道歉。

经过几个小时的拉扯和查看相同的答案。对我来说唯一有效的事情就是从多部分请求(Google文档)改为使用FormData

这是我的功劳

const metadata=JSON.stringify({
名称:myFile.name,
mimeType:myFile.type,
});
const requestData=new FormData();
append(“元数据”),新的Blob([metadata]{
类型:“application/json”
}));
requestData.append(“文件”,项[0]。文件);
const xhr=new XMLHttpRequest();
xhr.open(“POST”https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart");
const token=gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access\u令牌;
setRequestHeader(“Authorization”,Bearer${token}`);

发送(请求数据)“内容长度:26837”这是jpeg的正确大小吗?尝试对其进行base64编码。我使用并获取图像作为
base64
字符串,然后使用
parseInt(base64.replace(/=/g,”).Length*0.75)计算
内容长度。这看起来正确吗?如果你说你上传的字符串已经是base64编码的,那么content length应该是长度,你应该添加一个content transfer编码头。@Dale Zak感谢你的回复和测试。我很高兴你的问题解决了。也谢谢你。
{
    "kind": "drive#file",
    "id": "16dC3Fv2vKIur426INggTZ6GzDvyRUPrV",
    "name": "Untitled",
    "mimeType": "image/jpeg"
}
body += "--" + boundary
     + "\r\nContent-Disposition: form-data; name=" + key
     + "\r\nContent-type: " + formData[key].type
     + "\r\n\r\n" + formData[key].value + "\r\n";
body += "--" + boundary
     + "\r\nContent-Disposition: form-data; name=" + key
     + "\r\nContent-type: " + formData[key].type
     + (key == "file" ? "\r\nContent-Transfer-Encoding: base64" : "")  // Added
     + "\r\n\r\n" + formData[key].value + "\r\n";
headers: {
    "Content-Type": "multipart/related; boundary=" + boundary,
    'Content-Length': body.length
},
headers: {
    "Content-Type": "multipart/related; boundary=" + boundary,
},