Permalink
Browse files

Moving methods around and added documentation.

  • Loading branch information...
1 parent 54cb2c7 commit c742400d0b5d7613accca9ec775c95833de09710 @paracycle committed Apr 17, 2013
Showing with 45 additions and 19 deletions.
  1. +16 −13 lib/twitter/api/oauth.rb
  2. +28 −1 lib/twitter/client.rb
  3. +1 −5 lib/twitter/configurable.rb
View
@@ -5,9 +5,25 @@ module Twitter
module API
module OAuth
include Twitter::API::Utils
+
+ # Allows a registered application to obtain an OAuth 2 Bearer Token, which can be used to make API requests
+ # on an application's own behalf, without a user context.
+ #
+ # Only one bearer token may exist outstanding for an application, and repeated requests to this method
+ # will yield the same already-existent token until it has been invalidated.
+ #
+ # @see https://dev.twitter.com/docs/api/1.1/post/oauth2/token
+ # @rate_limited No
+ # @authentication Required
+ # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid.
+ # @return [Twitter::Token] The Bearer Token. token_type should be 'bearer'.
+ # @example Generate a Bearer Token
+ # client = Twitter::Client.new :consumer_key => "abc", :consumer_secret => 'def'
+ # bearer_token = client.token
def token
object_from_response(Twitter::Token, :bearer_request, "/oauth2/token", :grant_type => "client_credentials")
end
+
# Allows a registered application to revoke an issued OAuth 2 Bearer Token by presenting its client credentials.
#
# @see https://dev.twitter.com/docs/api/1.1/post/oauth2/invalidate_token
@@ -21,19 +37,6 @@ def token
def invalidate_token(access_token)
object_from_response(Twitter::Token, :post, "/oauth2/invalidate_token", :access_token => access_token)
end
-
- private
- def bearer_request(path, params={})
- connection.send(:post, path, params) do |request|
- request.headers[:accept] = "*/*"
- request.headers[:authorization] = "Basic #{encoded_bearer_token_credentials}"
- request.headers[:content_type] = "application/x-www-form-urlencoded; charset=UTF-8"
- end.env
- rescue Faraday::Error::ClientError
- raise Twitter::Error::ClientError
- rescue MultiJson::DecodeError
- raise Twitter::Error::DecodeError
- end
end
end
end
View
@@ -89,6 +89,18 @@ def request(method, path, params={}, signature_params=params)
raise Twitter::Error::DecodeError
end
+ def bearer_request(path, params={})
+ connection.send(:post, path, params) do |request|
+ request.headers[:authorization] = bearer_token_credentials_auth_header
+ request.headers[:content_type] = "application/x-www-form-urlencoded; charset=UTF-8"
+ request.headers[:accept] = "*/*"
+ end.env
+ rescue Faraday::Error::ClientError
+ raise Twitter::Error::ClientError
+ rescue MultiJson::DecodeError
+ raise Twitter::Error::DecodeError
+ end
+
# Returns a Faraday::Connection object
#
# @return [Faraday::Connection]
@@ -100,9 +112,24 @@ def connection
end
end
+ # Generates authentication header for a bearer token request
+ #
+ # @return [String]
+ def bearer_token_credentials_auth_header
+ credentials = Base64.strict_encode64("#{@consumer_key}:#{@consumer_secret}")
+ "Basic #{credentials}"
+ end
+
+ # Generates authentication header for when the :bearer_token is supplied
+ #
+ # @return [String]
+ def bearer_token_auth_header
+ "Bearer #{@bearer_token}"
+ end
+
def auth_header(method, path, params={})
if application_only_auth?
- "Bearer #{@bearer_token}"
+ bearer_token_auth_header
else
uri = URI(@endpoint + path)
SimpleOAuth::Header.new(method, uri, params, credentials)
@@ -52,11 +52,7 @@ def reset!
private
def application_only_auth?
- not @bearer_token.nil?
- end
-
- def encoded_bearer_token_credentials
- Base64.strict_encode64("#{@consumer_key}:#{@consumer_secret}")
+ !!@bearer_token
end
# @return [Hash]

0 comments on commit c742400

Please sign in to comment.