Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NonaSuomy/a62a9a125d61fc5df748066961702ac6 to your computer and use it in GitHub Desktop.
Save NonaSuomy/a62a9a125d61fc5df748066961702ac6 to your computer and use it in GitHub Desktop.
Get OAUTH2.0 access token for Google Voice with a shell-script
#!/bin/bash
##
## Authenticate with Google Voice
##
USAGE="`basename $0` {auth|refresh|token} ctx"
CTX_DIR=$HOME/.gvauth
CLIENT_ID="YOUR_CLIENTID_FROM_GOOGLE_DEVELOPER_CONSOLE"
CLIENT_SECRET="YOUR_CLIENTSECRET_FROM_GOOGLE_DEVELOPER_CONSOLE"
SCOPE="https://www.googleapis.com/auth/googletalk%20https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile"
ctx=default
function usage {
echo "Usage: `basename $0` [-h] [-c context] {init|token}"
exit
}
function age {
modified=`stat -c %X $1`
now=`date +%s`
expr $now - $modified
}
function refresh {
refresh_token=`cat $CTX_DIR/$ctx.refresh_token`
curl -si \
-d client_id=$CLIENT_ID \
-d client_secret=$CLIENT_SECRET \
-d refresh_token=$refresh_token \
-d grant_type=refresh_token \
https://www.googleapis.com/oauth2/v3/token > $CTX_DIR/$ctx.refresh
grep access_token $CTX_DIR/$ctx.refresh | sed -e 's/.*: "//' -e 's/",//' > $CTX_DIR/$ctx.access_token
}
while getopts :hc: opt ; do
case $opt in
c) ctx=$OPTARG ;;
h) usage ;;
esac
done
shift $(($OPTIND - 1))
cmd=$1 ; shift
mkdir -p $CTX_DIR
case $cmd in
init)
url=`curl -L -gsi \
-d scope=$SCOPE \
-d redirect_uri=urn:ietf:wg:oauth:2.0:oob \
-d response_type=code \
-d client_id=$CLIENT_ID\
https://accounts.google.com/o/oauth2/auth | \
grep Location: | \
sed 's/Location: //'`
echo $url | xclip -in -selection clipboard
echo $url
echo -n "Code? "
read code
curl -s \
-d client_id=$CLIENT_ID \
-d client_secret=$CLIENT_SECRET \
-d code=$code \
-d grant_type=authorization_code \
-d redirect_uri=urn:ietf:wg:oauth:2.0:oob \
https://www.googleapis.com/oauth2/v3/token > $CTX_DIR/$ctx.init
grep access_token $CTX_DIR/$ctx.init | sed -e 's/.*: "//' -e 's/",//' > $CTX_DIR/$ctx.access_token
grep refresh_token $CTX_DIR/$ctx.init | sed -e 's/.*: "//' -e 's/"//' > $CTX_DIR/$ctx.refresh_token
echo "Done"
;;
token)
if [ ! -f $CTX_DIR/$ctx.access_token ] ; then
echo "Unknown context: $ctx. Try initing first."
exit
fi
age=`age $CTX_DIR/$ctx.access_token`
if [ $age -gt 3600 ] ; then
refresh
fi
cat $CTX_DIR/$ctx.access_token
;;
*)
usage
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment