【问题标题】:Uploading file to Google Drive with Curl gives 401使用 Curl 将文件上传到 Google Drive 会出现 401
【发布时间】:2020-01-14 22:55:10
【问题描述】:

我正在尝试使用 Curl 将本地系统上的文件上传到我的 Google Drive 帐户。我去了 Google Developer Console 并启用了 Google Drive API,创建了一个API-KEY
这是我的卷发请求

curl -X POST -L \
    -H "Authorization: Bearer my_API-KEY" \
    -F "metadata={name : 'JUMBA'};type=application/json;charset=UTF-8" \
    -F "file=@my_file.json;type=application/zip" \
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"

但我收到此错误 -

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

文档也不是很清楚。这里有什么帮助吗?

【问题讨论】:

  • API 密钥不能用作访问令牌。此外,API 密钥不能用于 POST 和 PUT 方法。 API 密钥可用于使用 GET 方法请求公开共享的内容。那么例如,如何使用由 OAuth2 和/或服务帐户检索到的访问令牌?
  • @Tanaike 啊好吧。所以我应该去 OAuth 咨询屏幕并添加谷歌驱动器auth/drive 范围?但随后它要求提供强制性主页 url、隐私政策 url 等。我只是想以编程方式将文档上传到我的谷歌驱动器。我真的没有隐私政策网址和主页
  • 感谢您的回复。我注意到现在已经发布了答案。我认为它会解决您的问题。

标签: curl oauth google-api google-drive-api google-oauth


【解决方案1】:

“授权:承载 my_API-KEY”

授权标头应包含不记名令牌。不记名令牌是 JWT 访问令牌。 api 密钥是仅用于访问公共数据的公钥。您需要通过授权过程并请求访问令牌,然后才能执行此操作。

# Client id from Google Developer console
# Client Secret from Google Developer console
# Scope this is a space seprated list of the scopes of access you are requesting.

# Authorization link.  Place this in a browser and copy the code that is returned after you accept the scopes.
https://accounts.google.com/o/oauth2/auth?client_id=[Application Client Id]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=[Scopes]&response_type=code

# Exchange Authorization code for an access token and a refresh token.

curl \
--request POST \
--data "code=[Authentcation code from authorization link]&client_id=[Application Client Id]&client_secret=[Application Client Secret]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" \
https://accounts.google.com/o/oauth2/token

# Exchange a refresh token for a new access token.
curl \
--request POST \
--data 'client_id=[Application Client Id]&client_secret=[Application Client Secret]&refresh_token=[Refresh token granted by second step]&grant_type=refresh_token' \
https://accounts.google.com/o/oauth2/token

GoogleAuthenticationCurl.sh如果您在为 Google 云端硬盘更改它时遇到任何问题,请告诉我。你也可以查看我对这个相关问题的回答1841839

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2019-07-26
    • 2020-03-10
    相关资源
    最近更新 更多