Last active
February 3, 2022 20:49
-
-
Save untergrundbiber/1d51fa7465331b6b137d to your computer and use it in GitHub Desktop.
YouTube OAuth Bash
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
#!/bin/bash | |
#YouTube OAuth authentication for shell-based YT-tools like youtube-dl | |
#by untergrundbiber 2014 | |
#You need to register a app to obtaining clientId and clientSecret. | |
#https://developers.google.com/youtube/registering_an_application | |
#You can find the right scope here: https://developers.google.com/oauthplayground/ | |
#--- Settings --- | |
clientId="000000000000.apps.googleusercontent.com" | |
clientSecret="ABC" | |
scope="youtube" | |
#---------------- | |
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
tokenfile="$dir/authtoken" | |
function getCode { | |
echo "Please go to:" | |
echo | |
echo "https://accounts.google.com/o/oauth2/auth?client_id=$clientId&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/$scope&response_type=code" | |
echo | |
echo "after accepting, enter the code you are given:" | |
read code | |
} | |
function getAuthToken { | |
getCode | |
local cmd="curl https://accounts.google.com/o/oauth2/token --silent \ | |
--request POST --header \"Content-Type: application/x-www-form-urlencoded\"\ | |
-d client_id=$clientId -d client_secret=$clientSecret -d code=$code \ | |
-d redirect_uri=urn:ietf:wg:oauth:2.0:oob -d grant_type=authorization_code" | |
local authResponse=$($cmd) | |
access_token=$(echo $authResponse | awk -F"," '{print $1}' | awk -F":" '{print $2}' | sed s/\"//g | tr -d ' ') | |
refresh_token=$(echo $authResponse | awk -F"," '{print $4}' | awk -F":" '{print $2}' | sed s/\"//g | sed s/}// | tr -d ' ') | |
echo access_token , $access_token > $tokenfile | |
echo refresh_token , $refresh_token >> $tokenfile | |
#echo authResponse: $authResponse | |
echo access_token: $access_token | |
echo refresh_token: $refresh_token | |
} | |
function refreshAuthToken { | |
local cmd="curl https://accounts.google.com/o/oauth2/token --silent \ | |
--request POST --header \"Content-Type: application/x-www-form-urlencoded\"\ | |
-d client_id=$clientId -d client_secret=$clientSecret -d refresh_token=$refresh_token \ | |
-d grant_type=refresh_token" | |
local refreshResponse=$($cmd) | |
access_token=$(echo $refreshResponse | awk -F"," '{print $1}' | awk -F":" '{print $2}' | sed s/\"//g | tr -d ' ') | |
echo access_token , $access_token > $tokenfile | |
echo refresh_token , $refresh_token >> $tokenfile | |
#echo refreshResponse: $authResponse | |
echo access_token: $access_token | |
echo refresh_token: $refresh_token | |
} | |
function payload { | |
echo "Executing payload..." | |
#Put your payload here | |
#wget "http://gdata.youtube.com/feeds/api/users/USERNAME/newsubscriptionvideos?prettyprint=true&fields=entry(link[@rel='alternate'](@href))" --header "Authorization: OAuth $access_token" -O "ytsubs" | |
} | |
if [ -s $tokenfile ];then | |
access_token=$(cat $tokenfile | grep access_token | awk -F"," '{print $2}' | tr -d ' ') | |
refresh_token=$(cat $tokenfile | grep refresh_token | awk -F"," '{print $2}' | tr -d ' ') | |
else | |
echo "Token-file not found. Getting new token..." | |
getAuthToken | |
fi | |
EXPIRED=$(curl -s https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=$access_token | grep 'invalid_token') | |
if [ "$EXPIRED" ] | |
then | |
echo "AuthToken expired. Refreshing...." | |
refreshAuthToken | |
else | |
echo "AuthToken valid." | |
validResponse=$(curl -s https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=$access_token) | |
expires_in=`echo $validResponse | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w expires_in| cut -d":" -f2| sed -e 's/^ *//g' -e 's/ *$//g'` | |
echo "Token expires on:" `date --date "$expires_in sec" +"%x - %T"` | |
payload | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm having a problem if the token expires I get the following error which also then populates the authtoken file. Has anyone else seen this?
access_token , invalid_grant
refresh_token , https