0

我目前正在尝试创建一个上传系统,该系统将本地文件上传到谷歌驱动器。这是通过 Retool 完成的。我的PUT查询可以使用所需的 URI 正常返回,但我POST的查询失败了。经过大量研究,我一直无法确定原因。

这是我收到的错误消息(通过 JSON 格式化程序抛出以提高可读性):

{
   "data":{
      "statusCode":400,
      "error":"Bad Request",
      "message":"{\"message\":\"Not Found\"}",
      "data":{
         "message":"Not Found"
      },
      "metadata":{
         "request":{
            "url":"https://www.googleapis.com/upload/drive/v3/files?key=---sanitized---%26uploadType%3Dresumable%26upload_id%3D---sanitized---",
            "method":"PUT",
            "body":"UEsDBBQACAgIAHNbzlIAAAAAAAAAAAAAAAAYAAAAeGw...", //base64 string.
            "headers":{
               "User-Agent":"Retool/2.0 (+https://docs.tryretool.com/docs/apis)",
               "Content-Transfer-Encoding":"base64",
               "Content-Length":535487,
               "Authorization":"---sanitized---",
               "X-Retool-Forwarded-For":"104.129.199.15"
            }
         },
         "headers":{
            "x-guploader-uploadid":[
               "--READACTED--"
            ],
            "vary":[
               "Origin, X-Origin"
            ],
            "content-type":[
               "text/html; charset=UTF-8"
            ],
            "content-length":[
               "9"
            ],
            "date":[
               "Fri, 10 Sep 2021 01:58:07 GMT"
            ],
            "server":[
               "UploadServer"
            ],
            "alt-svc":[
               "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-T051=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\""
            ]
         },
         "status":404,
         "statusText":"Not Found"
      }
   },
   "errorData":"Not Found",
   "displayOptions":{
      "hideToast":false
   },
   "trigger":"NATURAL_FAILURE"
}

URI 与记录的 URIPUT_QUERY.headers.location[0]完美匹配。我的 API 密钥是有效的,我什至将 base64 字符串解码回一个 excel 文档以仔细检查它是否已损坏,但我已经不知道是什么原因导致了这种情况。

以下是我给正在使用的 Oauth2 令牌的范围:

https://www.googleapis.com/auth/docs 
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.appdata 
https://www.googleapis.com/auth/drive.file 
https://www.googleapis.com/auth/drive.metadata 
https://www.googleapis.com/auth/drive.metadata.readonly
https://www.googleapis.com/auth/drive.photos.readonly
https://www.googleapis.com/auth/drive.readonly 
https://www.googleapis.com/auth/drive.apps.readonly 
https://www.googleapis.com/auth/drive.activity
https://www.googleapis.com/auth/drive.activity.readonly

如果有帮助,我的POST请求看起来像..

{
   "request":{
      "url":"https://www.googleapis.com/upload/drive/v3/files?key=--sanitized--&uploadType=resumable",
      "method":"POST",
      "body":"{"name":"03 Mar 21 Conv--sanitized--.xlsx","parents":"[1NJ--sanitized--ka5W4Zz9Tc]"}",
      "headers":{
         "Content-Type":"application/json; charset=UTF-8",
         "User-Agent":"Retool/2.0 (+https://docs.tryretool.com/docs/apis)",
         "Authorization":"Bearer ---sanitized---",
         "X-Content-Length":535487,
         "X-Upload-Content-Type":"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
      }
   }
}
4

1 回答 1

0

发现错误。关于如何附加 url 子字符串的一些东西正在创建一个错误的链接。将代码更改为仅提取upload_id,现在它可以工作了。我希望可以帮助其他人的此代码的另一个错误是谷歌驱动器 api 可恢复PUT需要一个Content-Encoding标题,而不是Content-Transfer-Encoding. 否则上传将通过,并保留其所有元数据(名称、目录等)和大小,但无法正确打开。

于 2021-09-12T01:39:28.677 回答