Class: Iro::ApiController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/iro/api_controller.rb

Direct Known Subclasses

Iro::Api::StocksController

Instance Method Summary collapse

Instance Method Details

#oauth2_redirectObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/iro/api_controller.rb', line 12

def oauth2_redirect
  out = Schwab.post( "https://api.schwabapi.com/v1/oauth/token", {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    basic_auth: { username: SCHWAB_DATA[:key], password: SCHWAB_DATA[:secret] },
    body: {
      grant_type: 'authorization_code',
      code: params[:code].sub('%40', '@'),
      redirect_uri: SCHWAB_DATA[:redirect_url],
    },
  })
  out = out.parsed_response

  attrs = {
    schwab_access_token:  out['access_token'],
    schwab_refresh_token: out['refresh_token'],
    schwab_id_token:      out['id_token'],
  }
  # puts! attrs, 'attrs'

  profile = Wco::Profile.find_by email: '[email protected]'
  profile.update(attrs)
  profile.save!

  render json: { status: :ok }
end