Class: Twurl::AppOnlyOAuthClient

Inherits:
OAuthClient show all
Defined in:
lib/twurl/app_only_oauth_client.rb

Constant Summary collapse

AUTHORIZATION_FAILED_MESSAGE =
"Authorization failed. Check that your consumer key and secret are correct."

Constants inherited from OAuthClient

OAuthClient::METHODS, OAuthClient::OAUTH_CLIENT_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from OAuthClient

#username

Instance Method Summary collapse

Methods inherited from OAuthClient

#access_token, #authorized?, #build_request_from_options, #configure_http!, #consumer, #fetch_verify_credentials, #generate_authorize_url, has_oauth_options?, load_client_for_app_only_auth, load_client_for_non_profile_app_only_auth, load_client_for_username, load_client_for_username_and_consumer_key, load_default_client, load_from_options, load_new_client_from_oauth_options, load_new_client_from_options, #perform_pin_authorize_workflow, #pin_auth_parameters, rcfile, #to_hash, #user_agent, #verify_has_username

Constructor Details

#initialize(options = {}) ⇒ AppOnlyOAuthClient

Returns a new instance of AppOnlyOAuthClient.



11
12
13
14
15
# File 'lib/twurl/app_only_oauth_client.rb', line 11

def initialize(options = {})
  @consumer_key    = options['consumer_key']
  @consumer_secret = options['consumer_secret']
  @bearer_token    = options['bearer_token']
end

Instance Attribute Details

#bearer_tokenObject (readonly)

Returns the value of attribute bearer_token.



9
10
11
# File 'lib/twurl/app_only_oauth_client.rb', line 9

def bearer_token
  @bearer_token
end

#consumer_keyObject (readonly)

Returns the value of attribute consumer_key.



9
10
11
# File 'lib/twurl/app_only_oauth_client.rb', line 9

def consumer_key
  @consumer_key
end

#consumer_secretObject (readonly)

Returns the value of attribute consumer_secret.



9
10
11
# File 'lib/twurl/app_only_oauth_client.rb', line 9

def consumer_secret
  @consumer_secret
end

Instance Method Details

#exchange_credentials_for_access_tokenObject



21
22
23
24
25
26
27
# File 'lib/twurl/app_only_oauth_client.rb', line 21

def exchange_credentials_for_access_token
  response = fetch_oauth2_token
  if response.nil? || response[:access_token].nil?
    raise Exception, AUTHORIZATION_FAILED_MESSAGE
  end
  @bearer_token = response[:access_token]
end

#fetch_oauth2_tokenObject



69
70
71
72
73
74
75
76
# File 'lib/twurl/app_only_oauth_client.rb', line 69

def fetch_oauth2_token
  request = Net::HTTP::Post.new('/oauth2/token')
  request.body = URI.encode_www_form(request_data)
  request['user-agent'] = user_agent
  request['authorization'] = "Basic #{Base64.strict_encode64("#{consumer_key}:#{consumer_secret}")}"
  response = http_client.request(request).body
  JSON.parse(response,:symbolize_names => true)
end

#http_clientObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/twurl/app_only_oauth_client.rb', line 45

def http_client
  uri = URI.parse(Twurl.options.base_url)
  http = if Twurl.options.proxy
    proxy_uri = URI.parse(Twurl.options.proxy)
    Net::HTTP.new(uri.host, uri.port, proxy_uri.host, proxy_uri.port)
  else
    Net::HTTP.new(uri.host, uri.port)
  end
  set_http_client_options(http)
end

#needs_to_authorize?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/twurl/app_only_oauth_client.rb', line 37

def needs_to_authorize?
  bearer_token.nil?
end

#perform_request_from_options(options, &block) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/twurl/app_only_oauth_client.rb', line 29

def perform_request_from_options(options, &block)
  request = build_request_from_options(options)
  request['user-agent'] = user_agent
  request['authorization'] = "Bearer #{bearer_token}"

  http_client.request(request, &block)
end

#request_dataObject



41
42
43
# File 'lib/twurl/app_only_oauth_client.rb', line 41

def request_data
  {'grant_type' => 'client_credentials'}
end

#saveObject



17
18
19
# File 'lib/twurl/app_only_oauth_client.rb', line 17

def save
  self.class.rcfile.bearer_token(consumer_key, bearer_token)
end

#set_http_client_options(http) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/twurl/app_only_oauth_client.rb', line 56

def set_http_client_options(http)
  http.set_debug_output(Twurl.options.debug_output_io) if Twurl.options.trace
  http.read_timeout = http.open_timeout = Twurl.options.timeout || 60
  http.open_timeout = Twurl.options.connection_timeout if Twurl.options.connection_timeout
  # Only override if Net::HTTP support max_retries (since Ruby >= 2.5)
  http.max_retries = 0 if http.respond_to?(:max_retries=)
  if Twurl.options.ssl?
    http.use_ssl     = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  http
end