Skip to content

Instantly share code, notes, and snippets.

@lancehunt
Last active May 20, 2023 12:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancehunt/f0dd822b6087abaf31565c3bd3315172 to your computer and use it in GitHub Desktop.
Save lancehunt/f0dd822b6087abaf31565c3bd3315172 to your computer and use it in GitHub Desktop.
TradeStation OAuth2 Auth Code tester
#!/bin/bash
######################################################
# Stolen & Modified by Lance Hunt
# Author: Unknown
#
# Please let me know who the righful author is so I
# can give due credit
######################################################
echo '* Installing oauth2 Ruby gem'
gem install oauth2
echo '* Done installing oauth2 gem'
echo ''
apikey='********-****-****-****-************'
echo '* Enter your API Key:'
read apikey
apisecret='********-****-****-****-************'
echo '* Enter your API secret:'
read apisecret
site='https://api.tradestation.com'
echo '* Enter the OAuth2 site: (e.g. https://api.tradestation.com):'
read site
site=${site:-https://api.tradestation.com}
echo $site
authorizeurl='/v2/authorize/'
echo '* Enter the OAuth2 Authorize path: (e.g. /v2/authorize):'
read authorizeurl
authorizeurl=${authorizeurl:-/v2/authorize}
echo $authorizeurl
tokenurl='/v2/security/authorize/'
echo '* Enter the OAuth2 Token site: (e.g. /v2/security/authorize/):'
read tokenurl
tokenurl=${tokenurl:-/v2/security/authorize/}
echo $tokenurl
redirecturi='https://www.tradestation.com'
echo '* Enter the OAuth2 Redirect URI: (e.g. https://www.tradestation.com)'
read redirecturi
redirecturi=${redirecturi:-https://www.tradestation.com}
echo $redirecturi
authorize_url=`ruby -r oauth2 -e "puts OAuth2::Client.new(\"$apikey\", \"$apisecret\", :site=>\"$site\", :authorize_url=>\"$authorizeurl\", :token_url=>\"$tokenurl\").auth_code.authorize_url(:redirect_uri => \"$redirecturi\")"`
echo ''
echo "* You are about to be redirected to $site/$authorize_url"
echo ''
echo "***After authorizing, you'll be brought to $redirecturi. Copy the value from the 'code' querystring parameter 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
unescaped=`ruby -r cgi -e "puts CGI.unescape(\"$auth_code\")"`
#echo $unescaped
token=`ruby -r oauth2 -e "puts OAuth2::Client.new(\"$apikey\", \"$apisecret\", :site=>\"$site\", :authorize_url=>\"$authorizeurl\", :token_url=>\"$tokenurl\").auth_code.get_token('$escaped', :redirect_uri => \"$redirecturi\").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