使用Curl将文件上传到Google云端硬盘会显示401

时间:2019-09-13 04:24:44

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

我正在尝试使用Curl将本地系统上的文件上传到我的google驱动器帐户。我转到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"
 }
}

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

1 个答案:

答案 0 :(得分: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

的回答