Skip to content

Instantly share code, notes, and snippets.

@nsantorello
Created October 5, 2011 20:45
Show Gist options
  • Save nsantorello/1265651 to your computer and use it in GitHub Desktop.
Save nsantorello/1265651 to your computer and use it in GitHub Desktop.
Bash script to get the OAuth2 token for a user
#!/bin/bash
echo '* Installing oauth2 Ruby gem'
gem install oauth2
echo '* Done installing oauth2 gem'
echo ''
echo '* Enter your API ID:'
read apiid
echo '* Enter your API secret:'
read apisecret
echo '* Enter the OAuth2 site: (e.g. https://mysite.com):'
read site
authorize_url=`ruby -e "require 'rubygems'; require 'oauth2'; puts OAuth2::Client.new(\"$apiid\".strip, \"$apisecret\".strip, :site=>\"$site\".strip).auth_code.authorize_url(:redirect_uri => 'http://localhost/oauth2/callback')"`
echo ''
echo "* You are about to be redirected to $authorize_url"
echo ''
echo "***After authorizing, you'll be brought to a non-existent page. Copy the token in the URL you're redirected to (after 'code=') and paste below when prompted.***"
echo ''
echo '* Press ENTER to proceed!'
read
open $authorize_url
echo '* Enter your authorization code from the previous step:'
read auth_code
token=`ruby -e "require 'rubygems'; require 'oauth2'; puts OAuth2::Client.new(\"$apiid\".strip, \"$apisecret\".strip, :site=>\"$site\".strip).auth_code.get_token('$auth_code'.strip, :redirect_uri => 'http://localhost/oauth2/callback').token"`
echo ''
echo 'Your OAuth2 token is: '
echo $token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment