-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from Akianonymus/master
Use drive api v3 | Parallel Uploading | Sub folder upload | Improve logging | Pass shellcheck | Migrate to getopts | Share file/folder options | Format code
- Loading branch information
Showing
4 changed files
with
862 additions
and
334 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,94 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env bash | ||
|
||
# A simple cURL OAuth2 authenticator | ||
# A simple curl OAuth2 authenticator | ||
# | ||
# Usage: | ||
# ./google-oauth2.sh create - authenticates a user | ||
# ./google-oauth2.sh refresh <token> - gets a new token | ||
# | ||
# Set CLIENT_ID and CLIENT_SECRET and SCOPE | ||
# See SCOPES at https://developers.google.com/identity/protocols/oauth2/scopes#docsv1 | ||
|
||
#!/bin/bashset -e | ||
short_help() { | ||
echo -e "\nNo valid arguments provided." | ||
echo -e "Usage:\n" | ||
echo -e " ./google-oauth2.sh create - authenticates a user." | ||
echo -e " ./google-oauth2.sh refresh - gets a new access token." | ||
exit 0 | ||
} | ||
|
||
[ "$#" = "0" ] && short_help | ||
|
||
# Clear nth no. of line to the beginning of the line. | ||
clear_line() { | ||
echo -en "\033[""$1""A" | ||
echo -en "\033[2K" | ||
} | ||
|
||
[ "$1" = create ] || [ "$1" = refresh ] || short_help | ||
|
||
echo "Starting script.." | ||
|
||
CLIENT_ID="" | ||
CLIENT_SECRET="" | ||
SCOPE=${SCOPE:-"https://docs.google.com/feeds"} | ||
SCOPE="https://www.googleapis.com/auth/drive" | ||
REDIRECT_URI="urn:ietf:wg:oauth:2.0:oob" | ||
|
||
if [ -e $HOME/.googledrive.conf ] | ||
then | ||
. $HOME/.googledrive.conf | ||
fi | ||
# shellcheck source=/dev/null | ||
[ -e "$HOME"/.googledrive.conf ] && source "$HOME"/.googledrive.conf | ||
|
||
echo "Checking credentials.." | ||
|
||
if [ -z "$CLIENT_ID" ] | ||
then | ||
read -p "Client ID: " CLIENT_ID | ||
if [ -z "$CLIENT_ID" ]; then | ||
read -r -p "Client ID: " CLIENT_ID | ||
unset token | ||
echo "CLIENT_ID=$CLIENT_ID" >> $HOME/.googledrive.conf | ||
echo "CLIENT_ID=$CLIENT_ID" >> "$HOME"/.googledrive.conf | ||
fi | ||
|
||
if [ -z "$CLIENT_SECRET" ] | ||
then | ||
read -p "Client Secret: " CLIENT_SECRET | ||
if [ -z "$CLIENT_SECRET" ]; then | ||
read -r -p "Client Secret: " CLIENT_SECRET | ||
unset token | ||
echo "CLIENT_SECRET=$CLIENT_SECRET" >> $HOME/.googledrive.conf | ||
echo "CLIENT_SECRET=$CLIENT_SECRET" >> "$HOME"/.googledrive.conf | ||
fi | ||
|
||
sleep 1 | ||
clear_line 1 | ||
clear_line 1 | ||
echo "Required credentials set." | ||
sleep 1 | ||
|
||
# Method to extract data from json response | ||
function jsonValue() { | ||
KEY=$1 | ||
num=$2 | ||
awk -F"[:,}][^:\/\/]" '{for(i=1;i<=NF;i++){if($i~/\042'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/[,]*$//' | ||
jsonValue() { | ||
num="$2" | ||
grep \""$1"\" | sed "s/\:/\n/" | grep -v \""$1"\" | sed -e "s/\"\,//g" -e 's/["]*$//' -e 's/[,]*$//' -e 's/^[ \t]*//' -e s/\"// | sed -n "${num}"p | ||
} | ||
|
||
if [ "$1" == "create" ]; then | ||
RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/device/code" --data "client_id=$CLIENT_ID&scope=$SCOPE"` | ||
DEVICE_CODE=`echo "$RESPONSE" | jsonValue device_code` | ||
USER_CODE=`echo "$RESPONSE" | jsonValue user_code` | ||
URL=`echo "$RESPONSE" | jsonValue verification_url` | ||
echo "\nVisit the below URL, tap on allow and then enter the code obtained:" | ||
sleep 1 | ||
URL="https://accounts.google.com/o/oauth2/auth?client_id=$CLIENT_ID&redirect_uri=$REDIRECT_URI&scope=$SCOPE&response_type=code&prompt=consent" | ||
echo -e """$URL""\n" | ||
read -r -p "Enter the authorization code: " CODE | ||
|
||
echo -n "Go to $URL and enter $USER_CODE to grant access to this application. Hit enter when done..." | ||
read | ||
CODE="$(echo "$CODE" | tr -d ' ' | tr -d '[:blank:]' | tr -d '[:space:]')" | ||
if [ -n "$CODE" ]; then | ||
RESPONSE="$(curl -s --request POST --data "code=$CODE&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&redirect_uri=$REDIRECT_URI&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token)" | ||
|
||
RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/token" --data "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&code=$DEVICE_CODE&grant_type=http://oauth.net/grant_type/device/1.0"` | ||
ACCESS_TOKEN="$(echo "$RESPONSE" | jsonValue access_token)" | ||
REFRESH_TOKEN="$(echo "$RESPONSE" | jsonValue refresh_token)" | ||
|
||
ACCESS_TOKEN=`echo "$RESPONSE" | jsonValue access_token` | ||
REFRESH_TOKEN=`echo "$RESPONSE" | jsonValue refresh_token` | ||
|
||
echo "Access Token: $ACCESS_TOKEN" | ||
echo "Refresh Token: $REFRESH_TOKEN" | ||
echo "Access Token: $ACCESS_TOKEN" | ||
echo "Refresh Token: $REFRESH_TOKEN" | ||
else | ||
echo -e "\nNo code provided, run the script and try again" | ||
exit 1 | ||
fi | ||
elif [ "$1" == "refresh" ]; then | ||
REFRESH_TOKEN=$2 | ||
RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/token" --data "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&refresh_token=$REFRESH_TOKEN&grant_type=refresh_token"` | ||
|
||
ACCESS_TOKEN=`echo $RESPONSE | jsonValue access_token` | ||
|
||
echo "Access Token: $ACCESS_TOKEN" | ||
if [ -n "$REFRESH_TOKEN" ]; then | ||
RESPONSE="$(curl -s --request POST --data "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&refresh_token=$REFRESH_TOKEN&grant_type=refresh_token" https://accounts.google.com/o/oauth2/token)" | ||
ACCESS_TOKEN="$(echo "$RESPONSE" | jsonValue access_token)" | ||
echo "Access Token: $ACCESS_TOKEN" | ||
else | ||
echo "Refresh Token not set, use $0 create to generate one." | ||
fi | ||
fi |
Oops, something went wrong.