Class: GDataPlus::Authenticator::OAuth

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/gdata_plus/authenticator/o_auth.rb

Instance Method Summary collapse

Methods included from Common

#client

Constructor Details

#initialize(options = {}) ⇒ OAuth

Create a new instance.

Options

:consumer_key

(required)

:consumer_secret

(required)

TODO document optional options



17
18
19
20
21
22
23
24
25
# File 'lib/gdata_plus/authenticator/o_auth.rb', line 17

def initialize(options = {})
  required_options = [:consumer_key, :consumer_secret]
  optional_options = [:request_token, :request_secret, :access_token, :access_secret]
  options = Util.prepare_options(options, required_options, optional_options)

  (required_options + optional_options).each do |option_name|
    instance_variable_set :"@#{option_name}", options[option_name]
  end
end

Instance Method Details

#access_tokenObject



84
85
86
87
88
# File 'lib/gdata_plus/authenticator/o_auth.rb', line 84

def access_token
  if @access_token && @access_secret
    ::OAuth::AccessToken.new(consumer, @access_token, @access_secret)
  end
end

#consumerObject



27
28
29
30
31
32
33
# File 'lib/gdata_plus/authenticator/o_auth.rb', line 27

def consumer
  ::OAuth::Consumer.new(@consumer_key, @consumer_secret,
    :request_token_url => "https://www.google.com/accounts/OAuthGetRequestToken",
    :authorize_url => "https://www.google.com/accounts/OAuthAuthorizeToken",
    :access_token_url => "https://www.google.com/accounts/OAuthGetAccessToken"
  )
end

#fetch_access_token(oauth_verifier) ⇒ Object

Exchanges the request token for the access token. The “oauth_verifier” is passed as a URL parameter when Google redirects the client back to your oauth_callback URL.



73
74
75
76
77
78
79
80
81
82
# File 'lib/gdata_plus/authenticator/o_auth.rb', line 73

def fetch_access_token(oauth_verifier)
  access_token = request_token.get_access_token(:oauth_verifier => oauth_verifier)
  @access_token = access_token.token
  @access_secret = access_token.secret
  @request_token = nil
  @request_secret = nil
  access_token

  # TODO deal with error response
end

#fetch_request_token(options = {}, additional_oauth_options = {}, additional_request_params = {}) ⇒ Object

Arguments

options

(required) see options documentation below

additional_oauth_options

additional oauth params to pass to get_request_token; you will normally leave this blank

additional_request_params

additional params to pass with request; you will normally leave this blank

Options

:scope

(required) gdata scope; can be an Array or a String

:oauth_callback

(required) Google will redirect the user back to this URL after authentication



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gdata_plus/authenticator/o_auth.rb', line 49

def fetch_request_token(options = {}, additional_oauth_options = {}, additional_request_params = {})
  options = ::GDataPlus::Util.prepare_options(options, [:scope, :oauth_callback])

  additional_oauth_options.merge!(:oauth_callback => options[:oauth_callback])

  scope = options[:scope]
  scope = scope.join(" ") if scope.is_a? Array
  additional_request_params.merge!(:scope => scope)

  request_token = consumer.get_request_token(additional_oauth_options, additional_request_params)
  @request_token = request_token.token
  @request_secret = request_token.secret
  request_token
  # TODO deal with error response
end

#request_tokenObject



65
66
67
68
69
# File 'lib/gdata_plus/authenticator/o_auth.rb', line 65

def request_token
  if @request_token && @request_secret
    ::OAuth::RequestToken.new(consumer, @request_token, @request_secret)
  end
end

#sign_request(request) ⇒ Object

Adds authorization header to the specified Typeoeus::Request. The same request is also returned.

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gdata_plus/authenticator/o_auth.rb', line 91

def sign_request(request)
  raise ArgumentError, "request must be a Typeoeus::Request" unless request.is_a? ::Typhoeus::Request

  helper = ::OAuth::Client::Helper.new(request, {
    :consumer => consumer,
    :request_uri => request.url,
    :token => access_token
  })
  request.headers.merge!({"Authorization" => helper.header})
  request
end