Skip to content

Instantly share code, notes, and snippets.

@EscVector
Last active August 23, 2023 17:49
Show Gist options
  • Save EscVector/da320bb712844136a76e89167185f92b to your computer and use it in GitHub Desktop.
Save EscVector/da320bb712844136a76e89167185f92b to your computer and use it in GitHub Desktop.
Curl Fun with Google Drive - File Uploads
# Script to upload file to Google Drive using Device Authorization
# https://developers.google.com/identity/protocols/oauth2/limited-input-device
# Authorization is granted by entering the User_Code and accepting via https://google.com/device
# this is a manual step that must be completed
# Prerequisite - Google Project Device Credentials Client_id and Client_Secret and google.drive API Enabled
#!/bin/bash
# https://developers.google.com/identity/protocols/oauth2/limited-input-device
export GOOGLE_DRIVE=<SHARING_URL>
export CLIENT_ID=<YOUR_CLIENT_ID>
export CLIENT_SECRET=<YOUR_CLIENT_SECRET>
export SCOPE=https://www.googleapis.com/auth/drive.file
export VERIFY_DEVICE=`curl -d "client_id="$CLIENT_ID"&scope="$SCOPE https://oauth2.googleapis.com/device/code`
echo $VERIFY_DEVICE
USER_CODE=`echo $VERIFY_DEVICE | jq -r '.user_code'`
DEVICE_CODE=`echo $VERIFY_DEVICE | jq -r '.device_code'`
echo USER_CODE: $USER_CODE
# echo DEVICE_CODE: $DEVICE_CODE
echo https://google.com/device
read -p "Hit enter after entering user code > $USER_CODE" answer
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`
echo BEARER TOKEN: $BEARER
ACCESS_TOKEN=`echo $BEARER |jq -r '.access_token'`
echo ACCESS TOKEN: $ACCESS_TOKEN
echo ""
dir -ls
echo ""
if [ -z "$GOOGLE_DRIVE" ]
then
read -p "Enter Google Drive Location: " GOOGLE_DRIVE
else
echo Uploading to $GOOGLE_DRIVE
fi
echo ""
read -p "Enter File To Upload: " filename
echo ""
echo Uploading $filename
echo `curl -X POST -L \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-F "metadata={name :'$filename'};type=application/json;charset=UTF-8" \
-F "file="@$filename";type=application/zip" \
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"` | jq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment