{{tag>google cli terminal gdrive}} ====== Manipulate Google drive from CLI ====== We will use [[https://rclone.org/|rclone]] tool to duplicate folders in this example on a shared Google drive. First [[https://rclone.org/downloads/|download]] the standalone binary e.g. wget https://downloads.rclone.org/v1.66.0/rclone-v1.66.0-linux-amd64.zip unzip rclone-v1.66.0-linux-amd64.zip ===== Using simple login ===== Use this method when you need to manipulate your own drive and you can authenticate via browser e.g. on your local machine. Simply follow instructions from [[https://rclone.org/drive/#configuration|here]] ===== Using Google service accounts ===== This is useful when you need to run the rclone tool from some other server which doesn't have web browser installed to authenticate and you have paid Google Workspace account and when you want to synchronise files onto machines that don't have actively logged-in users, for example build machines. Instructions are adapted from [[https://rclone.org/drive/#service-account-support|here]]. == 1. Create service account == * To create a service account and obtain its credentials, go to the Google Developer Console. * You must have a project - create one if you don't. * Then go to "IAM & admin" -> "Service Accounts". * Use the "Create Service Account" button. Fill in "Service account name" and "Service account ID" with something that identifies your client. * Select "Create And Continue". Step 2 and 3 are optional. * These credentials are what rclone will use for authentication. If you ever need to remove access, press the "Delete service account key" button. * Click on created service account and create new json key which you will store for later in a local file e.g. rclone-sac.json {{ :wiki:screenshots:gc_create_key1.png?600 |}} == 2. Enable the Google Drive API == Go to https://console.developers.google.com/apis/api/drive.googleapis.com/overview and enable the API. (under Google Console > APIs & Services) {{ :wiki:screenshots:enable_google_api.png?600 |}} == 3. Allow API access to example.com Google Drive == Contact your Google Workspace admin and allow access for the service account's Client ID [[https://developers.google.com/identity/protocols/oauth2/service-account#delegatingauthority|howto here]] == 4. Create new config and test == After that is done follow [[https://rclone.org/drive/#3-configure-rclone-assuming-a-new-install|steps 3 and 4]] to make sure it works ===== Use Google OAuth account ===== This is another way to authorize the rclone. Just follow the steps [[https://rclone.org/drive/#making-your-own-client-id|here]]. Either way should work. **Some screenshots** {{ :wiki:screenshots:google_oauth_client_id1.png?400 |Creating new OAuth client id 1}} {{ :wiki:screenshots:google_oauth_client_id2.png?400 |Creating new OAuth client id 2}} {{ :wiki:screenshots:google_oauth_consent1.png?400 |Choose Internal if you have a Google Workspace account}} {{ :wiki:screenshots:google_oauth_scope1.png?400 |Selecting scopes}} {{ :wiki:screenshots:google_credentials.png?400 |Both auth methods active}} ====== Example commands ====== Show help: ./rclone -h List existing remotes: ./rclone listremotes Copy folder inside the my_remote using service account method. This reads the secrets from downloaded json; ./rclone -v --drive-impersonate me@gmail.com copy 'my_remote:folder1' 'my_remote:folder1_copy' Copy folder inside the my_remote using Google OAuth account. The client's id's and secret is also obtained from the json you get from finishing the OAuth steps and can be used on cmd line. ./rclone -v --drive-impersonate me@gmail.com --drive-client-id 'xxxxxxx194eq.apps.googleusercontent.com' --drive-client-secret 'xxxx' copy 'my_remote:folder1' 'my_remote:folder1_copy' ====== Tested on ====== * rclone-v1.66.0 * Google services 2024-05 ====== See also ====== ====== References ====== * https://cloud.google.com/iam/docs/service-accounts-create