Create a new consumer instance by passing it a configuration hash:
@consumer = OAuth::Consumer.new(key, secret, {
:site => "http://term.ie",
:scheme => :header,
:http_method => :post,
:request_token_path => "/oauth/example/request_token.php",
:access_token_path => "/oauth/example/access_token.php",
:authorize_path => "/oauth/example/authorize.php"
})
Start the process by requesting a token
@request_token = @consumer.get_request_token session[:request_token] = @request_token redirect_to @request_token.authorize_url
When user returns create an access_token
@access_token = @request_token.get_access_token
@photos=@access_token.get('/photos.xml')
# File lib/oauth/consumer.rb, line 77
77: def initialize(consumer_key, consumer_secret, options = {})
78: @key = consumer_key
79: @secret = consumer_secret
80:
81: # ensure that keys are symbols
82: @options = @@default_options.merge(options.inject({}) do |opts, (key, value)|
83: opts[key.to_sym] = value
84: opts
85: end)
86: end
# File lib/oauth/consumer.rb, line 254
254: def access_token_path
255: @options[:access_token_path]
256: end
# File lib/oauth/consumer.rb, line 275
275: def access_token_url
276: @options[:access_token_url] || site + access_token_path
277: end
# File lib/oauth/consumer.rb, line 279
279: def access_token_url?
280: @options.has_key?(:access_token_url)
281: end
Creates and signs an http request. It’s recommended to use the Token classes to set this up correctly
# File lib/oauth/consumer.rb, line 189
189: def create_signed_request(http_method, path, token = nil, request_options = {}, *arguments)
190: request = create_http_request(http_method, path, *arguments)
191: sign!(request, token, request_options)
192: request
193: end
# File lib/oauth/consumer.rb, line 108
108: def get_access_token(request_token, request_options = {}, *arguments, &block)
109: response = token_request(http_method, (access_token_url? ? access_token_url : access_token_path), request_token, request_options, *arguments, &block)
110: OAuth::AccessToken.from_hash(self, response)
111: end
Makes a request to the service for a new OAuth::RequestToken
@request_token = @consumer.get_request_token
To include OAuth parameters:
@request_token = @consumer.get_request_token \ :oauth_callback => "http://example.com/cb"
To include application-specific parameters:
@request_token = @consumer.get_request_token({}, :foo => "bar")
TODO oauth_callback should be a mandatory parameter
# File lib/oauth/consumer.rb, line 127
127: def get_request_token(request_options = {}, *arguments, &block)
128: # if oauth_callback wasn't provided, it is assumed that oauth_verifiers
129: # will be exchanged out of band
130: request_options[:oauth_callback] ||= OAuth::OUT_OF_BAND unless request_options[:exclude_callback]
131:
132: if block_given?
133: response = token_request(http_method,
134: (request_token_url? ? request_token_url : request_token_path),
135: nil,
136: request_options,
137: *arguments, &block)
138: else
139: response = token_request(http_method, (request_token_url? ? request_token_url : request_token_path), nil, request_options, *arguments)
140: end
141: OAuth::RequestToken.from_hash(self, response)
142: end
The HTTP object for the site. The HTTP Object is what you get when you do Net::HTTP.new
# File lib/oauth/consumer.rb, line 94
94: def http
95: @http ||= create_http
96: end
The default http method
# File lib/oauth/consumer.rb, line 89
89: def http_method
90: @http_method ||= @options[:http_method] || :post
91: end
# File lib/oauth/consumer.rb, line 283
283: def proxy
284: @options[:proxy]
285: end
Creates, signs and performs an http request. It’s recommended to use the OAuth::Token classes to set this up correctly. request_options take precedence over consumer-wide options when signing
a request.
arguments are POST and PUT bodies (a Hash, string-encoded parameters, or
absent), followed by additional HTTP headers.
@consumer.request(:get, '/people', @token, { :scheme => :query_string })
@consumer.request(:post, '/people', @token, {}, @person.to_xml, { 'Content-Type' => 'application/xml' })
# File lib/oauth/consumer.rb, line 154
154: def request(http_method, path, token = nil, request_options = {}, *arguments)
155: if path !~ /^\//
156: @http = create_http(path)
157: _uri = URI.parse(path)
158: path = "#{_uri.path}#{_uri.query ? "?#{_uri.query}" : ""}"
159: end
160:
161: # override the request with your own, this is useful for file uploads which Net::HTTP does not do
162: req = create_signed_request(http_method, path, token, request_options, *arguments)
163: return nil if block_given? and yield(req) == :done
164: rsp = http.request(req)
165: # check for an error reported by the Problem Reporting extension
166: # (http://wiki.oauth.net/ProblemReporting)
167: # note: a 200 may actually be an error; check for an oauth_problem key to be sure
168: if !(headers = rsp.to_hash["www-authenticate"]).nil? &&
169: (h = headers.select { |hdr| hdr =~ /^OAuth / }).any? &&
170: h.first =~ /oauth_problem/
171:
172: # puts "Header: #{h.first}"
173:
174: # TODO doesn't handle broken responses from api.login.yahoo.com
175: # remove debug code when done
176: params = OAuth::Helper.parse_header(h.first)
177:
178: # puts "Params: #{params.inspect}"
179: # puts "Body: #{rsp.body}"
180:
181: raise OAuth::Problem.new(params.delete("oauth_problem"), rsp, params)
182: end
183:
184: rsp
185: end
# File lib/oauth/consumer.rb, line 237
237: def request_endpoint
238: return nil if @options[:request_endpoint].nil?
239: @options[:request_endpoint].to_s
240: end
# File lib/oauth/consumer.rb, line 246
246: def request_token_path
247: @options[:request_token_path]
248: end
TODO this is ugly, rewrite
# File lib/oauth/consumer.rb, line 259
259: def request_token_url
260: @options[:request_token_url] || site + request_token_path
261: end
# File lib/oauth/consumer.rb, line 263
263: def request_token_url?
264: @options.has_key?(:request_token_url)
265: end
# File lib/oauth/consumer.rb, line 242
242: def scheme
243: @options[:scheme]
244: end
Sign the Request object. Use this if you have an externally generated http request object you want to sign.
# File lib/oauth/consumer.rb, line 224
224: def sign!(request, token = nil, request_options = {})
225: request.oauth!(http, self, token, options.merge(request_options))
226: end
Return the signature_base_string
# File lib/oauth/consumer.rb, line 229
229: def signature_base_string(request, token = nil, request_options = {})
230: request.signature_base_string(http, self, token, options.merge(request_options))
231: end
# File lib/oauth/consumer.rb, line 233
233: def site
234: @options[:site].to_s
235: end
Creates a request and parses the result as url_encoded. This is used internally for the RequestToken and AccessToken requests.
# File lib/oauth/consumer.rb, line 196
196: def token_request(http_method, path, token = nil, request_options = {}, *arguments)
197: response = request(http_method, path, token, request_options, *arguments)
198: case response.code.to_i
199:
200: when (200..299)
201: if block_given?
202: yield response.body
203: else
204: # symbolize keys
205: # TODO this could be considered unexpected behavior; symbols or not?
206: # TODO this also drops subsequent values from multi-valued keys
207: CGI.parse(response.body).inject({}) do |h,(k,v)|
208: h[k.strip.to_sym] = v.first
209: h[k.strip] = v.first
210: h
211: end
212: end
213: when (300..399)
214: # this is a redirect
215: response.error!
216: when (400..499)
217: raise OAuth::Unauthorized, response
218: else
219: response.error!
220: end
221: end
Contains the root URI for this site
# File lib/oauth/consumer.rb, line 99
99: def uri(custom_uri = nil)
100: if custom_uri
101: @uri = custom_uri
102: @http = create_http # yike, oh well. less intrusive this way
103: else # if no custom passed, we use existing, which, if unset, is set to site uri
104: @uri ||= URI.parse(site)
105: end
106: end
Instantiates the http object
# File lib/oauth/consumer.rb, line 290
290: def create_http(_url = nil)
291:
292:
293: if !request_endpoint.nil?
294: _url = request_endpoint
295: end
296:
297:
298: if _url.nil? || _url[0] =~ /^\//
299: our_uri = URI.parse(site)
300: else
301: our_uri = URI.parse(_url)
302: end
303:
304:
305: if proxy.nil?
306: http_object = Net::HTTP.new(our_uri.host, our_uri.port)
307: else
308: proxy_uri = proxy.is_a?(URI) ? proxy : URI.parse(proxy)
309: http_object = Net::HTTP.new(our_uri.host, our_uri.port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
310: end
311:
312: http_object.use_ssl = (our_uri.scheme == 'https')
313:
314: if @options[:ca_file] || CA_FILE
315: http_object.ca_file = @options[:ca_file] || CA_FILE
316: http_object.verify_mode = OpenSSL::SSL::VERIFY_PEER
317: http_object.verify_depth = 5
318: else
319: http_object.verify_mode = OpenSSL::SSL::VERIFY_NONE
320: end
321: http_object
322: end
create the http request object for a given http_method and path
# File lib/oauth/consumer.rb, line 325
325: def create_http_request(http_method, path, *arguments)
326: http_method = http_method.to_sym
327:
328: if [:post, :put].include?(http_method)
329: data = arguments.shift
330: end
331:
332: headers = arguments.first.is_a?(Hash) ? arguments.shift : {}
333:
334: case http_method
335: when :post
336: request = Net::HTTP::Post.new(path,headers)
337: request["Content-Length"] = 0 # Default to 0
338: when :put
339: request = Net::HTTP::Put.new(path,headers)
340: request["Content-Length"] = 0 # Default to 0
341: when :get
342: request = Net::HTTP::Get.new(path,headers)
343: when :delete
344: request = Net::HTTP::Delete.new(path,headers)
345: when :head
346: request = Net::HTTP::Head.new(path,headers)
347: else
348: raise ArgumentError, "Don't know how to handle http_method: :#{http_method.to_s}"
349: end
350:
351: if data.is_a?(Hash)
352: form_data = {}
353: data.each {|k,v| form_data[k.to_s] = v if !v.nil?}
354: request.set_form_data(form_data)
355: elsif data
356: if data.respond_to?(:read)
357: request.body_stream = data
358: if data.respond_to?(:length)
359: request["Content-Length"] = data.length
360: elsif data.respond_to?(:stat) && data.stat.respond_to?(:size)
361: request["Content-Length"] = data.stat.size
362: else
363: raise ArgumentError, "Don't know how to send a body_stream that doesn't respond to .length or .stat.size"
364: end
365: else
366: request.body = data.to_s
367: request["Content-Length"] = request.body.length
368: end
369: end
370:
371: request
372: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.