Class: ConstantContact::Auth::OAuth2
- Inherits:
-
Object
- Object
- ConstantContact::Auth::OAuth2
- Defined in:
- lib/constantcontact/auth/oauth2.rb
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#props ⇒ Object
Returns the value of attribute props.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
Instance Method Summary collapse
-
#get_access_token(code) ⇒ String
Obtain an access token.
-
#get_authorization_url(server = true, state = nil) ⇒ String
Get the URL at which the user can authenticate and authorize the requesting application.
-
#get_token_info(access_token) ⇒ Object
Get an information about an access token.
-
#initialize(opts = {}) ⇒ Object
constructor
Class constructor.
Constructor Details
#initialize(opts = {}) ⇒ Object
Class constructor
19 20 21 22 23 24 25 26 |
# File 'lib/constantcontact/auth/oauth2.rb', line 19 def initialize(opts = {}) @client_id = opts[:api_key] || Util::Config.get('auth.api_key') @client_secret = opts[:api_secret] || Util::Config.get('auth.api_secret') @redirect_uri = opts[:redirect_url] || Util::Config.get('auth.redirect_uri') if @client_id.nil? || @client_id == '' || @client_secret.nil? || @client_secret.nil? || @redirect_uri.nil? || @redirect_uri == '' raise ArgumentError.new "Either api_key, api_secret or redirect_uri is missing in explicit call or configuration." end end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id
10 11 12 |
# File 'lib/constantcontact/auth/oauth2.rb', line 10 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret
10 11 12 |
# File 'lib/constantcontact/auth/oauth2.rb', line 10 def client_secret @client_secret end |
#props ⇒ Object
Returns the value of attribute props
10 11 12 |
# File 'lib/constantcontact/auth/oauth2.rb', line 10 def props @props end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri
10 11 12 |
# File 'lib/constantcontact/auth/oauth2.rb', line 10 def redirect_uri @redirect_uri end |
Instance Method Details
#get_access_token(code) ⇒ String
Obtain an access token
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/constantcontact/auth/oauth2.rb', line 55 def get_access_token(code) params = { :grant_type => Util::Config.get('auth.authorization_code_grant_type'), :client_id => @client_id, :client_secret => @client_secret, :code => code, :redirect_uri => @redirect_uri } url = [ Util::Config.get('auth.base_url'), Util::Config.get('auth.token_endpoint') ].join response_body = '' begin response = RestClient.post(url, params) response_body = JSON.parse(response) rescue => e response_body = e.respond_to?(:response) && e.response ? JSON.parse(e.response) : {'error' => '', 'error_description' => e.} end if response_body['error_description'] error = response_body['error_description'] raise Exceptions::OAuth2Exception, error end response_body end |
#get_authorization_url(server = true, state = nil) ⇒ String
Get the URL at which the user can authenticate and authorize the requesting application
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/constantcontact/auth/oauth2.rb', line 33 def (server = true, state = nil) response_type = server ? Util::Config.get('auth.response_type_code') : Util::Config.get('auth.response_type_token') params = { :response_type => response_type, :client_id => @client_id, :redirect_uri => @redirect_uri } if state params[:state] = state end [ Util::Config.get('auth.base_url'), Util::Config.get('auth.authorization_endpoint'), '?', Util::Helpers.http_build_query(params) ].join end |
#get_token_info(access_token) ⇒ Object
Get an information about an access token
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/constantcontact/auth/oauth2.rb', line 91 def get_token_info(access_token) params = { :access_token => access_token } url = [ Util::Config.get('auth.base_url'), Util::Config.get('auth.token_info') ].join response = RestClient.post(url, params) response_body = JSON.parse(response) end |