upload zip file to google drive using curl

You can use Drive API v3 to upload the zip file. The modified curl code is as follows.

curl -X POST -L \
    -H "Authorization: Bearer `cat /tmp/token.txt`" \
    -F "metadata={name : 'backup.zip'};type=application/json;charset=UTF-8" \
    -F "[email protected];type=application/zip" \
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"

In order to use this, please include https://www.googleapis.com/auth/drive in the scope.


The answer above works fine and was the command I used in uploading my file to Google Drive using Curl. However, I didn't understand what scope was and all of the initial setup required to make this command work. Hence, for documentation purposes. I'll give a second answer.

Valid as at the time of writing...

Visit the Credentials page and create a new credential (this is assuming you have created a project). I created credentials for TVs and Limited devices, so the work flow was similar to:

Create credentials > OAuth client ID > Application Type > TVs and Limited Input devices > Named the client > Clicked Create.

After doing this, I was able to copy the Client ID and Client Secret when viewing the newly created credential.

NB: Only the variables with double asterisk from the Curl commands should be replaced.

Next step was to run the Curl command:

curl -d "client_id=**client_id**&scope=**scope**" https://oauth2.googleapis.com/device/code

Scope in this situation can be considered to be the kind of access you intend to have with the credential having the inputted client_id. More about scope from the docs For the use case in focus, which is to upload files, the scope chosen was https://www.googleapis.com/auth/drive.file.

On running the curl command above, you'll get a response similar to:

{ "device_code": "XXXXXXXXXXXXX", "user_code": "ABCD-EFGH",
"expires_in": 1800, "interval": 5, "verification_url": "https://www.google.com/device" }

Next step is to visit the verification_url in the response in your browser, provide the user_code and accept requests for permissions. You will be presented with a code when all prompts have been followed, this code wasn't required for the remaining steps (but there may be some reasons to use it for other use cases).

Next step is to use the Curl command:

curl -d client_id=**client_id** -d client_secret=**client_secret** -d device_code=**device_code** -d grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code https://accounts.google.com/o/oauth2/token

You will get a response similar to:

{ "access_token": "XXXXXXXXX", "expires_in": 3599,
"refresh_token": "XXXXXXXXX", "scope": "https://www.googleapis.com/auth/drive.file", "token_type": "Bearer" }

Now you can use the access token and follow the accepted answer with a Curl command similar to:

curl -X POST -L \
    -H "Authorization: Bearer **access_token**" \
    -F "metadata={name : 'backup.zip'};type=application/json;charset=UTF-8" \
    -F "[email protected];type=application/zip" \
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"