34

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 Google Drive API, but that seems very restricted to using a web/OAuth flow.

All I need to do is upload Word, Excel files etc. to Google Drive, but I need to do this in an entirely automated manner, with no user interface of any kind. I wish to write a command line application, which can run on cron or whatever, and not require human intervention via the web etc.

I'd rather move away from Documents List API, as I don't want to get burned when they eventually turn it off, I'd like to use a supported API which Google are not going get rid of any time soon. Does this exist?

jww
  • 97,681
  • 90
  • 411
  • 885
Garry
  • 623
  • 1
  • 7
  • 11
  • 2
    What's wrong with the Google Drive API: https://developers.google.com/drive/ ? You can easily interface with it in a command line program. – Sean Dawson Sep 16 '12 at 11:44
  • Here is a [nice](http://olivermarshall.net/how-to-upload-a-file-to-google-drive-from-the-command-line/) article. – ViniciusArruda Jun 30 '18 at 21:01

4 Answers4

10

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.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • Thank you for this, it's not ideal for me, as we're talking about using the software of machines which simply do not have graphical user interfaces, but if that's the situation, we're in, then I have to work around it. Thanks. – Garry Sep 16 '12 at 17:32
  • 3
    the authorisation process and the drive access don't need to be on the same device. Therefore you can have a servlet which performs the authorisation flow, and wins a refresh token. You could then securely transfer that refresh token to the machine and you're good to go. Alternatively, you can win a refresh token using the OAuth playground. See http://stackoverflow.com/questions/19766912/how-do-i-authorise-an-app-web-or-installed-without-user-intervention-canonic – pinoyyid Aug 01 '16 at 18:51
8

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

vozman
  • 1,198
  • 1
  • 14
  • 19
  • To iInstall `rclone` on Linux/macOS/BSD systems, run: `curl https://rclone.org/install.sh | sudo bash` – Anwarvic Feb 11 '22 at 12:52
7

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
jww
  • 97,681
  • 90
  • 411
  • 885
SiB
  • 101
  • 1
  • 1
4

Since the gdrive tool is no longer maintained. I found an official and better way.

Using curl.

  1. Install Curl

sudo apt install curl

  1. Create your project credentials on google console. Make an account first if you don't have any.

Create credentials > Configure OAth consent screen (if needed) > Application type > TV and limited input devices > Save your client id and a client secret.

  1. Verify the device

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.

  1. Save the "device_code" and "user_code" values.
  2. Get Bearer code

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"
}
  1. Save the "access_token" value.

  2. 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

Ritwik
  • 521
  • 7
  • 17