Google Drive API使用curl调用创建文件而不是文件夹

时间:2019-07-25 15:28:03

标签: javascript rest curl google-drive-api

我已经收到access_token,也可以打API到Google云端硬盘的电话。

curl -v -H 'Authorization: Bearer ya29.GltQBdS3y-QkCbKhO5K5pO19bBPaHh-KXZJw2ftw2tyxqSXEAYo06FQfHPSQmFtuN7XAlzb9k7cQw2ZkyBsKDcpX91_N3LObEw_Zgj8OvpMwCCPNK1UDjT' -H 'Content-Type: application/json' -d '{"uploadType": "simple", "title": "mb", "mimeType": "application/vnd.google-apps.folder"}' https://www.googleapis.com/upload/drive/v3/files

但是,在Google云端硬盘上创建的是一个文件,其中我的请求正文为文件内容 enter image description here
enter image description here

使用curl的整个有效负载如下所示:

*   Trying 2607:f8b0:400a:800::200a...
* TCP_NODELAY set
* Connected to www.googleapis.com (2607:f8b0:400a:800::200a) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/cert.pem
  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-ECDSA-CHACHA20-POLY1305
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=US; ST=California; L=Mountain View; O=Google LLC; CN=*.googleapis.com
*  start date: Jun 18 08:35:41 2019 GMT
*  expire date: Sep 10 08:16:00 2019 GMT
*  subjectAltName: host "www.googleapis.com" matched cert's "*.googleapis.com"
*  issuer: C=US; O=Google Trust Services; CN=Google Internet Authority G3
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7fcc1c006600)
> POST /upload/drive/v3/files HTTP/2
> Host: www.googleapis.com
> User-Agent: curl/7.54.0
> Accept: */*
> Authorization: Bearer ya29.GltQBzZdS3y-QkCbO5K5pO19bBPaHh-KXZJw2ftw2tyxqSXEAYo06FQ6QmFtuN7XAlzb9k7cQw2ZkUZkyBscpX91_N3LObEw_Zgj8OvpMwCCPNK1UDjT
> Content-Type: application/json
> Content-Length: 89
>
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
* We are completely uploaded and fine
< HTTP/2 200
< x-guploader-uploadid: AEnB2UpR0hclmVI75f-2hK49hCe3OpA3dkW7GR5PCgd4DgOOkeL2KSoVQDgC-bThMDzXvqcYTaWU3St1pruugvcpst9yaslA
< vary: Origin
< vary: X-Origin
< content-type: application/json; charset=UTF-8
< cache-control: no-cache, no-store, max-age=0, must-revalidate
< pragma: no-cache
< expires: Mon, 01 Jan 1990 00:00:00 GMT
< date: Thu, 25 Jul 2019 15:23:02 GMT
< content-length: 124
< server: UploadServer
< alt-svc: quic=":443"; ma=2592000; v="46,43,39"
<
{
 "kind": "drive#file",
 "id": "1MZ4s2ikDqTU5tu-5LDuyMx84TwHgw7",
 "name": "Untitled",
 "mimeType": "application/json"
}
* Connection #0 to host www.googleapis.com left intact

我在做什么错了?

谢谢

2 个答案:

答案 0 :(得分:1)

  • 您要使用curl命令在Google云端硬盘中创建新文件夹。
  • 您已经可以使用Drive API。
    • 您的访问令牌可以用于POST方法。

如果我的理解是正确的,那么该修改如何?

修改点:

  • 创建新文件夹后,可以使用https://www.googleapis.com/drive/v3/files作为端点。
  • 使用Drive API v3时,可以通过name的属性来设置文件夹名称。

当上面的修改反映到您的curl命令时,它变为如下。

修改的curl命令:

curl \
  -H 'Authorization: Bearer ### your access token ###' \
  -H 'Content-Type: application/json' \
  -d '{"name": "mb", "mimeType": "application/vnd.google-apps.folder"}' \
  https://www.googleapis.com/drive/v3/files

参考:

如果我误解了您的问题,而这不是您想要的结果,我深表歉意。

答案 1 :(得分:0)

为什么您有Google文件夹mime类型?

“ mimeType”:“ application / vnd.google-apps.folder”}'

上传和从Excel转换为电子表格的示例 curl -X POST -sSL \         -H“授权:承载[您的访问令牌]”         -F“元数据= {\                      名称:“ [Google云端硬盘上的文件名]”,\                      mimeType:“ application / vnd.google-apps.spreadsheet” \                      }; type = application / json; charset = UTF-8“ \         -F“ file = @ [您的Excel文件]; type = application / vnd.ms-excel” \         “ https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart