谷歌硬盘上传API可恢复REST返回400个错误请求

谷歌硬盘上传API可恢复REST返回400个错误请求,rest,google-drive-api,Rest,Google Drive Api,我正试图通过这个链接跟踪google drive api的可恢复媒体上传。 我的第一个请求很好地通过了,我在位置头中得到了第二个url。 然而,当我进行文档中给出的第二个PUT调用时,它抛出了一个错误的请求 Request 1: POST https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable Request Header 1: Content-Type: application/json Autho

我正试图通过这个链接跟踪google drive api的可恢复媒体上传。

我的第一个请求很好地通过了,我在位置头中得到了第二个url。 然而,当我进行文档中给出的第二个PUT调用时,它抛出了一个错误的请求

Request 1: 

POST https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable

Request Header 1: 

Content-Type: application/json
Authorization: Bearer ya..
X-Upload-Content-Type: text/plain
X-Upload-Content-Length: 112
Host: www.googleapis.com
Content-Length: 112
Body 

{'title' : 'bravo122314.txt', 'mimeType': 'text/plain', 'parents' : [{'id': '0Bw5JasxEfsasa8NNsdU5dU123iVHc'}] } 


Response Header 1:
Location: https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&upload_id=ASM
Date: Tue, 03 Dec 2013 21:07:40 GMT
Server: HTTP Upload Server Built on Nov 15 2013 16:02:54 (1384560174)
Content-Length: 0
Content-Type: text/html; charset=UTF-8
这是下面的请求

Request 2: 
PUT https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&upload_id=ASM

Request Header 2: 
Authorization: Bearer ya..
Content-Type: text/plain


Response 2:

{
"error":{
"errors":[
{
"domain":"global",
"reason":"badRequest",
"message":"Invalid Upload Request"
}
],
"code":400,
"message":"Invalid Upload Request"
}
}
以前有人遇到过这个问题吗?我是不是少了一些头球

尝试“上传类型”使用“媒体”确保可以先上传,然后尝试“可恢复”

注意id,您可以看到ref

参考:

尝试“上传类型”使用“媒体”确保可以先上传,然后尝试“可恢复”

注意id,您可以看到ref

参考:

在第二个请求中,您还需要设置内容长度头,它将如下所示:

Request 2: 
PUT https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&upload_id=ASM

Request Header 2: 
Authorization: Bearer ya..
Content-Type: text/plain
Content-Length: 112

Request Body 2: 
{ bytes of file to upload }

在第二个请求中,您还需要设置Content Length标头,它将如下所示:

Request 2: 
PUT https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&upload_id=ASM

Request Header 2: 
Authorization: Bearer ya..
Content-Type: text/plain
Content-Length: 112

Request Body 2: 
{ bytes of file to upload }

希望你现在已经解决了这个问题,但由于我刚刚开始尝试,我将在这里添加一些我个人的发现

我首先想到的是
X-Upload-Content-Length:112
应该是你在
PUT
中要发送的文件的长度,而不是你第一次请求的长度

我还需要在我的
PUT
中添加
“Content Encoding”:“base64”
作为标题,然后确保将blob编码为base64。我一直在用javascript做这件事,所以我用
btoa(reader.result)

这两个标题都需要与
PUT
请求上的
内容类型
内容长度
标题匹配:

X-Upload-Content-Type: text/plain
X-Upload-Content-Length: 112

出于安全原因,您不能在javascript请求中自行设置
内容类型,但浏览器应该能够处理该问题。

希望您现在已经解决了这个问题,但由于我刚刚开始尝试这样做,我将在这里添加一些个人发现

我首先想到的是
X-Upload-Content-Length:112
应该是你在
PUT
中要发送的文件的长度,而不是你第一次请求的长度

我还需要在我的
PUT
中添加
“Content Encoding”:“base64”
作为标题,然后确保将blob编码为base64。我一直在用javascript做这件事,所以我用
btoa(reader.result)

这两个标题都需要与
PUT
请求上的
内容类型
内容长度
标题匹配:

X-Upload-Content-Type: text/plain
X-Upload-Content-Length: 112
出于安全原因,您不能在javascript请求中自行设置
内容类型
,但浏览器应该能够处理该问题