Javascript required
Skip to content Skip to sidebar Skip to footer

Upload to Google Drive Unix Terminal Curl

Google Bulldoze File Upload Using The Terminal

How to employ Google Drive in Linux

In this article, I share with you a solution that authenticates a user to upload file data to a Google Bulldoze repository. If you are interested in knowing how to do it, keep reading to detect out.

Before we begin I would like to describe your attention to the following resources I stole. I mean borrowed from that were very helpful.

The first author is Daniel Ellis and the second author is Athiththan Kathirgamasegaran.

Prerequisite

  1. Your Google account has been signed upward to use Google Bulldoze.
  2. Have access to Google Cloud Platform
  3. Take cURL installed.
  • To check, type the following command "which ringlet". Y'all can install curl with:
                          Debian and Ubuntu Systems
sudo apt install whorl
Ruby hat, CentOS, Fedora
sudo rpm install scroll
sudo yum install gyre
sudo dnf install curlicue
OpenSUSE
sudo zypper install curl
Arch Linux
pacman -Due south whorl

What is the roll control in Linux?
The curl control is a tool to transfer data from or to a server. It offers a busload of useful tricks like proxy support, user authentication, FTP upwards‐load, HTTP post, SSL connections, cookies, file transfer resume, Metalink, and more.

Outset Steps
Research OAuth and methods for accessing Google APIs. Google'due south cloud file storage service provides users with a personal storage space, chosen My Bulldoze.

In that location are several OAuth methods available to access Google Drive only the one nosotros are focusing on is client ID and undercover. A client ID is used to place a single app to Google's OAuth servers.

This diagram below shows the relationship between your Google Drive app, Google Drive, and Google Drive API:

Epitome Source

Procedure

To employ client ID and client hugger-mugger to access Google Bulldoze, create a new project.

Log in to Google Deject Platform.
Select the drop downward listing to the left side of the search bar.
Select New Project ":",

Give your project a suitable name and click on Create

Once the project has been created, a notification will appear confirming it has been successful.

Create Credentials
Navigate to the credentials panel (on the left) then select create credentials from the acme. From the driblet down menu select OAuth client ID option.

Next, configure the consent screen. When you use OAuth 2.0 for authorization, your app requests authorizations for one or more scopes of access from a Google Business relationship. Google displays a consent screen including a summary of the project and its policies and the requested scopes of access.

From the two options presented for user type, choose Internal or External and click Create. I have called internal as it limits the scope to users to inside your organization.

You should meet a similar page to the following image. The application proper name you provide will be displayed to the user on the consent screen when asking for permissions to use your Google accounts and services.

Enter the app name or ID you used when creating the projection.

Calculation an image or support email is optional.

Click on Save and Keep.
Have the default telescopic configuration click on Save and Continue.

The following window will display the consent screen summary details.

Back at the dashboard, Navigate to Credentials
Select Create Credentials from the meridian.
When asked for the application blazon, select TVs and Express input devices.

Finally, this will generate a customer id and client undercover. You have the pick to download the credentials in a JSON format. It will contain additional information such as redirect uri, which is substantially your username and password so shop it somewhere secure.

Enable Google Bulldoze API

Navigate to the Dashboard and select Enable APIs and Services from the top. Use the API Library search box to find and enable "Google Bulldoze API" .

Note: To ensure successful execution of the script there must exist a compressed binder present in the same directory as the script. For this demo we are only concerned with the upload process.

Verify The Device

First nosotros need to verify our device. Open a terminal on your automobile and run the following commands.

            VERIFY_DEVICE=`curl -d "client_id="$CLIENT_ID"&telescopic=$SCOPE" https://oauth2.googleapis.com/device/code`          

Yous should see a similar response by running the echo control.

            echo $VERIFY_DEVICE
{ "device_code": "XXXXXXXXXXXXXXXXXXXXXXXX", "user_code": "NRDJ-SHDX", "expires_in": 1800, "interval": 5, "verification_url": "https://www.google.com/device" }

Adjacent, you will need to visit the verification_url in the response to a higher place and you will demand to type the user to user_code to complete verification.

You are so required to accept requests for permissions.

Get The Bearer Code

Earlier an upload is possible, nosotros need to excerpt the device_code from the response of the initial coil command. The jq option is used to filter the json response via a pipe. The device_code value is stored in the DEVICE_CODE variable.

            DEVICE_CODE=`repeat $VERIFY_DEVICE | jq -r '.device_code'`
echo $DEVICE_CODE

The client ID and secret are stored as global variables, as they are used several times throughout information technology is quicker and easier to use. They tin exist accessed from Credentials in APIs & Services.

            BEARER=`roll -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`

The response is as follows:

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

Next, we need the access_token as this is needed after for uploading the file.

            ACCESS_TOKEN=`echo $BEARER | jq -r '.access_token'`              
echo $ACCESS_TOKEN

Upload File

For the purposes of this demo we are echoing the response to the concluding.

The folder ID is located in the URL of the Drive binder. For example, https://drive.google.com/drive/folders/1dyGErghjTY3Z467YfdGA7mfUH32g", then the Folder ID would exist " 1dyGErghjTY3Z467YfdGA7mfUH32g" .

            echo `whorl -X Postal service -L \
-H 'Authorization: Bearer '${ACCESS_TOKEN} \
-F 'metadata={proper noun : "backup.aught", parents : ["<drive-binder-url>"]};type=application/json;charset=UTF-8' \
-F 'file=@backup.zip;blazon=application/zip' \
'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart'`

Hither, nosotros are performing a multipart upload request to transport metadata along with the data to upload. Nosotros are using this option as the data we're transferring is < 5MB. Refer to the client library documentation for boosted details on how to use each of the upload types here.

Congratulations!!. You have just pushed your first file to your Google Bulldoze

Putting it all Together

When you have tried all the commands in the final it is time to put it together into a unmarried script. Depending on the organisation you lot are using you volition have different text editor options (Vim, Vi, Nano, gedit, etc). The following script was created using the Nano editor.

            #!/bin/bash            # This script enables the user to upload a file to Google Drive            # fail fast
set -east pipefail
CLIENT_ID=<your-generated-client-id>
CLIENT_SECRET=<your-generated-client-secret>
Scope=https://www.googleapis.com/auth/bulldoze.file # This is the URL we'll send the user to first to get their potency
# verify device
VERIFY_DEVICE=`coil -d "client_id="$CLIENT_ID"&scope=$SCOPE" https://oauth2.googleapis.com/device/code`
echo $VERIFY_DEVICE
# excerpt device_code value from the json response using jq
DEVICE_CODE=`echo $VERIFY_DEVICE | jq -r '.device_code'`
repeat $DEVICE_CODE
# pause the script to give the user time to navigate to verification_url and enter the user_code.
sleep 25
# get bearer lawmaking
BEARER=`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`
#repeat $BEARER # extract access_token value from the json response using jq
ACCESS_TOKEN=`echo $BEARER | jq -r '.access_token'`
#echo $ACCESS_TOKEN
echo `coil -X POST -Fifty \
-H 'Authorization: Bearer '${ACCESS_TOKEN} \
-F 'metadata={proper noun : "fill-in.zip", parents : ["<Google-Drive-Folder-URL-Hither>"]};type=application/json;charset=UTF-8' \
-F 'file=@backup.null;type=awarding/zip' \
'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart'`

Determination

That's a wrap, you lot now take the power to authenticate with Google APIs using this demo with file upload. I'm sure there is someone out there with a cleaner solution to push files from a computer to a Google Bulldoze repository. Give thanks you for taking the time to read this post and I hope you got something from it. If y'all did, I would be delighted to get your feedback.
I would besides challenge you to have this solution and fully automate the process, then the user_code is automatically posted to the verification_url. Thus, removing the one transmission footstep.

babbagephompecture.blogspot.com

Source: https://medium.com/@ianhutch90/google-drive-file-upload-using-the-terminal-3652ee90a6f6