Module: TDAmeritrade::Authentication

Includes:
Util
Included in:
Client
Defined in:
lib/tdameritrade/authentication.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

handle_api_error, parse_json_response, response_success?

Instance Attribute Details

#access_tokenObject (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_atObject (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_codeObject (readonly)

Returns the value of attribute authorization_code.



9
10
11
# File 'lib/tdameritrade/authentication.rb', line 9

def authorization_code
  @authorization_code
end

#client_idObject (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_uriObject (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_tokenObject (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_atObject (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(authorization_grant_code)
  # headers = { 'Content-Type': 'application/x-www-form-urlencoded' } # turns out didn't need this
  params = {
    'grant_type': 'authorization_code',
    'access_type': 'offline',
    'code': authorization_grant_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_tokenObject 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