1

I am trying to upload a file on my local system to my google drive account using Curl. I went to Google Developer Console and enabled Google Drive API, created an API-KEY.
This is my Curl Request

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"

But I get this Error -

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

The documentation is also not very clear. Any help here?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Ashwin
  • 12,691
  • 31
  • 118
  • 190
  • API key cannot be used as the access token. And also, API key cannot be used for POST and PUT method. API key can be used for requesting the publicly shared contents using GET method. So for example, how about using the access token retrieved by OAuth2 and/or service account? – Tanaike Sep 13 '19 at 05:12
  • @Tanaike ah okay. So I should go to the OAuth Consult Screen and add google drive `auth/drive` scope? But then it asks for a mandatory home page url , privacy policy url etc. I am just trying to programatically upload documents to my google drive. I don't really have a privacy policy url and home page – Ashwin Sep 13 '19 at 05:53
  • Thank you for replying. I noticed that an answer has already been posted now. I think that it will resolve your issue. – Tanaike Sep 13 '19 at 11:52

1 Answers1

1

"Authorization: Bearer my_API-KEY"

An authorization header should contain a bearer token. A bearer token is a JWT access token. An api key is a public key used for accessing public data only. You need to go though the authorization process and request an access token before you are going to be able to do this.

# 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 Let me know if you have any issues altering it for Google drive. You can also check my answer on this related question 1841839

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449