【问题标题】:Google Drive API creates a File instead of a Folder using curl callGoogle Drive API 使用 curl 调用创建文件而不是文件夹
【发布时间】:2019-07-26 06:22:18
【问题描述】:

我已经能够获得 access_token 并且我还能够拨打 Google Drive 的 API 电话。

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 Drive 上创建的是一个以我的请求正文作为文件内容的文件

使用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

我做错了什么?

谢谢

【问题讨论】:

    标签: javascript rest curl google-drive-api


    【解决方案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
    

    参考:

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

    【讨论】:

    • 这很有帮助!谢谢@Tanaike。 2 个不正确的部分是 - 1. 使用端点(我不知道该选择哪个端点)和 2. 在有效负载中使用 name 而不是 title。当我修复它们时,它开始起作用了!
    • 不相关的问题,你知道acess_token过期后如何刷新吗?
    • @daydreamer 感谢您的回复。我很高兴你的问题得到了解决。关于刷新访问令牌,当您拥有刷新令牌时,您可以刷新访问令牌。但此时访问令牌的字符串已更改,请注意这一点。关于如何使用刷新令牌刷新访问令牌,此线程可能很有用。 stackoverflow.com/a/45991141/7108653
    • 谢谢@Tanaike,我会试试看的!
    【解决方案2】:

    为什么你有 google 文件夹 mime 类型?

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

    上传并从 Excel 转换为电子表格的示例 卷曲 -X POST -SSL \ -H“授权:承载[您的访问令牌]”\ -F“元数据={\ name : '[Google Drive 上的文件名]', \ 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"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多