I am looking to write a server application to upload files to Google Drive. I was previously using the Documents List API, but I see that is deprecated. I\'d like to move to the
Your application needs the user's permission to use the API against his files. That authorisation needs to happen using web based Oauth. The result of that authorisation is your server app ends up with a refresh token, which it can store. At any time, your app can convert that refresh token to an access token and access the drive files.
So, provided you accept that you need to do a one-time authorisation, you can achieve what you are looking for.
Here are the instructions for Windows, Linux and MacOS using GitHub | gdrive.
$ gdrive list
Go to the url... enter the oauth verification code... OK
$ gdrive upload file
$ gdrive mkdir UploadDir
ID_of_UploadDir
$ gdrive sync upload LocalDir ID_of_UploadDir
Since the gdrive tool is no longer maintained. I found an official and better way.
Using curl.
sudo apt install curl
Create credentials > Configure OAth consent screen (if needed) > Application type > TV and limited input devices > Save your client id and a client secret.
curl -d "client_id=<client_id>&scope=https://www.googleapis.com/auth/drive.file" https://oauth2.googleapis.com/device/code
Expected response:
{"device_code": "<long string>",
"user_code": "xxx-xxx-xxx",
"expires_in": 1800,
"interval": 5,
"verification_url": "https://www.google.com/device"}
Then go to the https://www.google.com/device
--> Enter "user_code"
--> Give relevant permissions.
"device_code"
and "user_code"
values.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
Expected Output:
{
"access_token": ".....",
"expires_in": 3599,
"refresh_token": "....",
"scope": "https://www.googleapis.com/auth/drive.file",
"token_type": "Bearer"
}
Save the "access_token"
value.
Start uploading
curl -X POST -L -H "Authorization: Bearer <enter access_token here>" -F "metadata={name :'filename.zip'};type=application/json;charset=UTF-8" -F "file=@filename.zip;type=application/zip" "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
CAUTION
Make zip archives of your files before uploading. The above code works for archived files. I uploaded 10gb archive with the above method successfully.
Source
gdrive
solution is not working as of now(login problems). So you can now use rclone
. You can install it with
conda install -c conda-forge rclone
Then follow the configuration doc https://rclone.org/drive/
After configuration you will be able to copy to google drive with this command(stat flags are for progress bar)
rclone copy <filename> remote: --stats-one-line -P --stats 2s
rclone has many backends included so you can upload not only to google drive