Module: TDAmeritrade::Authentication
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#access_token_expires_at ⇒ Object
readonly
Returns the value of attribute access_token_expires_at.
-
#authorization_code ⇒ Object
readonly
Returns the value of attribute authorization_code.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#redirect_uri ⇒ Object
readonly
Returns the value of attribute redirect_uri.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
-
#refresh_token_expires_at ⇒ Object
readonly
Returns the value of attribute refresh_token_expires_at.
Instance Method Summary collapse
-
#get_access_tokens(authorization_grant_code) ⇒ Object
This is the OAuth code retrieved from your browser window, first step in OAuth needed to retrieve the tokens.
- #get_new_access_token ⇒ Object (also: #refresh_access_token)
- #update_tokens(args = {}) ⇒ Object
Methods included from Util
handle_api_error, parse_json_response, response_success?
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
9 10 11 |
# File 'lib/tdameritrade/authentication.rb', line 9 def access_token @access_token end |
#access_token_expires_at ⇒ Object (readonly)
Returns the value of attribute access_token_expires_at.
9 10 11 |
# File 'lib/tdameritrade/authentication.rb', line 9 def access_token_expires_at @access_token_expires_at end |
#authorization_code ⇒ Object (readonly)
Returns the value of attribute authorization_code.
9 10 11 |
# File 'lib/tdameritrade/authentication.rb', line 9 def @authorization_code end |
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
9 10 11 |
# File 'lib/tdameritrade/authentication.rb', line 9 def client_id @client_id end |
#redirect_uri ⇒ Object (readonly)
Returns the value of attribute redirect_uri.
9 10 11 |
# File 'lib/tdameritrade/authentication.rb', line 9 def redirect_uri @redirect_uri end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
9 10 11 |
# File 'lib/tdameritrade/authentication.rb', line 9 def refresh_token @refresh_token end |
#refresh_token_expires_at ⇒ Object (readonly)
Returns the value of attribute refresh_token_expires_at.
9 10 11 |
# File 'lib/tdameritrade/authentication.rb', line 9 def refresh_token_expires_at @refresh_token_expires_at end |
Instance Method Details
#get_access_tokens(authorization_grant_code) ⇒ Object
This is the OAuth code retrieved from your browser window, first step in OAuth needed to retrieve the tokens
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tdameritrade/authentication.rb', line 13 def get_access_tokens() # headers = { 'Content-Type': 'application/x-www-form-urlencoded' } # turns out didn't need this params = { 'grant_type': 'authorization_code', 'access_type': 'offline', 'code': , 'client_id': client_id, 'redirect_uri': redirect_uri } response = HTTParty.post( 'https://api.tdameritrade.com/v1/oauth2/token', body: params ) unless response_success?(response) raise TDAmeritrade::Error::TDAmeritradeError.new( "Unable to retrieve access tokens from API - #{response.code} - #{response.body}" ) end update_tokens(parse_json_response(response)) end |
#get_new_access_token ⇒ Object Also known as: refresh_access_token
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tdameritrade/authentication.rb', line 36 def get_new_access_token params = { grant_type: 'refresh_token', refresh_token: refresh_token, access_type: 'offline', client_id: client_id, redirect_uri: redirect_uri } response = HTTParty.post( 'https://api.tdameritrade.com/v1/oauth2/token', body: params ) update_tokens(parse_json_response(response)) end |
#update_tokens(args = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tdameritrade/authentication.rb', line 54 def update_tokens(args={}) gem_error(args[:error]) if args.has_key?(:error) @access_token = args[:access_token] @refresh_token = args[:refresh_token] @access_token_expires_at = Time.now + (args[:expires_in] || 0) @refresh_token_expires_at = Time.now + (args[:refresh_token_expires_in] || 0) args end |