Module: Schwab::OAuth
- Defined in:
- lib/schwab/oauth.rb
Overview
OAuth 2.0 authentication helpers for Schwab API
Class Method Summary collapse
-
.authorization_url(client_id:, redirect_uri:, state: nil, config: nil) ⇒ String
Generate the authorization URL for the OAuth 2.0 flow.
-
.get_token(code:, client_id:, client_secret:, redirect_uri:, config: nil) ⇒ Hash
Exchange an authorization code for access and refresh tokens.
-
.refresh_token(refresh_token:, client_id:, client_secret:, config: nil) ⇒ Hash
Refresh an access token using a refresh token.
Class Method Details
.authorization_url(client_id:, redirect_uri:, state: nil, config: nil) ⇒ String
Generate the authorization URL for the OAuth 2.0 flow
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/schwab/oauth.rb', line 18 def (client_id:, redirect_uri:, state: nil, config: nil) config ||= Schwab.configuration || Configuration.new state ||= SecureRandom.hex(16) params = { response_type: "code", client_id: client_id, redirect_uri: redirect_uri, state: state, } uri = URI(config.) uri.query = URI.encode_www_form(params) uri.to_s end |
.get_token(code:, client_id:, client_secret:, redirect_uri:, config: nil) ⇒ Hash
Exchange an authorization code for access and refresh tokens
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/schwab/oauth.rb', line 42 def get_token(code:, client_id:, client_secret:, redirect_uri:, config: nil) config ||= Schwab.configuration || Configuration.new client = oauth2_client(client_id: client_id, client_secret: client_secret, config: config) token = client.auth_code.get_token( code, redirect_uri: redirect_uri, headers: { "Content-Type" => "application/x-www-form-urlencoded" }, ) parse_token_response(token) end |
.refresh_token(refresh_token:, client_id:, client_secret:, config: nil) ⇒ Hash
Refresh an access token using a refresh token
62 63 64 65 66 67 68 69 70 |
# File 'lib/schwab/oauth.rb', line 62 def refresh_token(refresh_token:, client_id:, client_secret:, config: nil) config ||= Schwab.configuration || Configuration.new client = oauth2_client(client_id: client_id, client_secret: client_secret, config: config) token = OAuth2::AccessToken.new(client, nil, refresh_token: refresh_token) new_token = token.refresh! parse_token_response(new_token) end |