Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding Bearer Token support #387

Merged
merged 9 commits into from

3 participants

Ufuk Kayserilioglu Steve Agalloco Erik Michaels-Ober
Ufuk Kayserilioglu
Collaborator
  • Added :bearer_token to the list of configuration parameters
  • The default value of the bearer_token is read from the environment variable
  • Requests send the "Bearer AAA..." type authentication header if a bearer token exists
  • Added tests to verify that bearer token implementation works as intended

PS: I am very new to this project, so please disregard if I have done anything wrong... ;)

paracycle added some commits
Ufuk Kayserilioglu paracycle Adding Bearer Token support
- Added :bearer_token to the list of configuration parameters
- The default value of the bearer_token is read from the environment
  variable
- Requests send the "Bearer AAA..." type authentication header if a
  bearer token exists
- Added tests to verify that bearer token implementation works as
  intended
2ec6142
Ufuk Kayserilioglu paracycle Adding oath2/token method
- I had to replicate the functionality of the request method to
  accomodate for the abnormal way we have to make the request
- Configurable can supply the base64 encoded bearer token credential
54cb2c7
Ufuk Kayserilioglu
Collaborator

Added the oauth2/token method implementation as well, but I am not very pleased with it. Could someone give me feedback please?

Steve Agalloco
Collaborator

This looks really good to me actually, nicely done.

lib/twitter/configurable.rb
@@ -49,6 +51,13 @@ def reset!
alias setup reset!
private
+ def application_only_auth?
+ not @bearer_token.nil?
Steve Agalloco Collaborator
stve added a note

stylistic preference for me, but i loathe double negatives like this, perhaps change to:

!!@bearer_token
Ufuk Kayserilioglu Collaborator

OK, that sounds perfectly fine, will change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/twitter/configurable.rb
@@ -49,6 +51,13 @@ def reset!
alias setup reset!
private
+ def application_only_auth?
+ not @bearer_token.nil?
+ end
+
+ def encoded_bearer_token_credentials
Steve Agalloco Collaborator
stve added a note

Twitter::Configurable doesn't seem like the right place for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/twitter/api/oauth.rb
@@ -19,6 +21,19 @@ module OAuth
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
Steve Agalloco Collaborator
stve added a note

I understand why you put this here, but feels like this is also misplaced. Does it make sense to put bearer_request and encoded_bearer_token_credentials on Twitter::Client alongside the rest of the request-based methods.

Ufuk Kayserilioglu Collaborator

If I understand you correctly, you propose I move bearer_request and encoded_bearer_token_credentials to client.rb as private methods. I could certainly do that.

However, I am not happy supporting two separate pathways that both call connection.send in ways that are fundamentally very similar. My initial attempt was to include an extra parameter (e.g :bearer_token_request => true in the call, ultimately, to request method. Then inside the request method to check for bearer request (by calling params.delete(:bearer_token_request) and in return to change the block given to connection.send. That would certainly localize connection calls but will also incur an extra hash lookup and if statement for every connection request, and even worse where most of the calls will not even be using the alternative pathway. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Ufuk Kayserilioglu
Collaborator

OK. I have refactored the code and added tests and docs for the token method.

I still have the following questions:

  1. Is the name token alright? I am somehow uncomfortable with it, since it looks too generic (especially when you see it as client.token. Should we rename it to generate_token or something similar?
  2. Adding a separate request method still feels dirty. It also reduced coverage to 99.96% since we will need to test the exception pathways in the new request method. As I suggested above in the diff comment, should we unify the two methods and do a conditional check to decide on which block to use? I worry about performance but we are already doing a check in auth_header for every call even though most of the calls would probably not use it.
Erik Michaels-Ober
Owner

Is the name token alright? I am somehow uncomfortable with it, since it looks too generic (especially when you see it as client.token. Should we rename it to generate_token or something similar?

I think token is okay, since there's only one type of token you can request from Twitter.

Adding a separate request method still feels dirty. It also reduced coverage to 99.96% since we will need to test the exception pathways in the new request method. As I suggested above in the diff comment, should we unify the two methods and do a conditional check to decide on which block to use? I worry about performance but we are already doing a check in auth_header for every call even though most of the calls would probably not use it.

Ideally, you could get this into a single conditional statement without needing a duplicate request method. If this means moving the auth_header logic inside the request method, so be it.

Other than that, this pull request looks very good to me.

Ufuk Kayserilioglu
Collaborator

@sferik Thank you for your comments. Should I try to unify the request methods, then, also including the auth_header logic inside?

In your original email to the Google Group, you mention being able to initialize the library with just client_token and client_secret. This would imply that the library should call the token method internally at some point. Do you think this the right way to do it, and, if yes, where do you think this should happen? My guess is yes and, when constructing the Auth header for the request.

Erik Michaels-Ober
Owner

Should I try to unify the request methods, then, also including the auth_header logic inside?

Yes, please.

In your original email to the Google Group, you mention being able to initialize the library with just client_token and client_secret. This would imply that the library should call the token method internally at some point. Do you think this the right way to do it, and, if yes, where do you think this should happen? My guess is yes and, when constructing the Auth header for the request.

When checking for credentials, the code should check if client_token and client_secret are set (and @bearer_token is nil). If this is the case, the client should call the token method and set the @bearer_token instance variable in the client to use for all future requests. Make sense?

lib/twitter/api/oauth.rb
((5 lines not shown))
+ # 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
Erik Michaels-Ober Owner
sferik added a note

Maybe alias this method to bearer_token?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
paracycle added some commits
Ufuk Kayserilioglu paracycle Unifying the request method of Twitter::Client
- The request method is using a helper method request_setup that returns
  a Proc which sets up the correct headers for the current request.
- Each type of different authentication header is being returned by a
  separate private method and they all have tests.
15baf20
Ufuk Kayserilioglu paracycle Adding bearer_token as an alias to token afb9301
Ufuk Kayserilioglu

@sferik Is this what you meant, or did I misinterpret you?

Owner

Yes, this is all I meant. It's a minor addition but I think it's nice to have.

Ufuk Kayserilioglu
Collaborator

@sferik I just pushed the request method unification, please tell me if there are any functional and/or stylistic errors.

As to automatically acquiring BT given CT and CS: I couldn't find a place where credentials are checked. There is a credentials? method that doesn't seem to be used by the rest of the code, except for tests. Besides, if we do the check it has to be while the client is trying to make a request. If we call the token method at this point and it raises an error, the user of the library may be confused since the error they receive will not be related to the method they were trying to access.

If you think this pull request is ready just for adding bearer_token support, maybe you can merge this and then we can discuss and I can implement a proper solution for automagically getting BT from CT and CS.

lib/twitter/client.rb
((8 lines not shown))
+ # @param params [Hash]
+ # @return [Proc]
+ def request_setup(method, path, params)
+ if params.delete :bearer_token_request
+ Proc.new 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] = '*/*' # It is important we set this, otherwise we get an error.
+ end
+ elsif application_only_auth?
+ Proc.new do |request|
+ request.headers[:authorization] = bearer_auth_header
+ end
+ else
+ Proc.new do |request|
+ uri = URI(@endpoint + path)
Steve Agalloco Collaborator
stve added a note

this doesn't appear to be used

Ufuk Kayserilioglu Collaborator

duh, you are right. that was a left over from a refactor. will remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/twitter/client.rb
((18 lines not shown))
uri = URI(@endpoint + path)
SimpleOAuth::Header.new(method, uri, params, credentials)
end
-
- end
+end
Erik Michaels-Ober Owner
sferik added a note

Minor point but this end should be indented.

Ufuk Kayserilioglu Collaborator

definitely, good catch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/twitter/configurable.rb
@@ -1,10 +1,11 @@
require 'forwardable'
require 'twitter/error/configuration_error'
+require 'base64'
Erik Michaels-Ober Owner
sferik added a note

This require statement belongs in client.rb, where the Base64 module is used.

Ufuk Kayserilioglu Collaborator

fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Erik Michaels-Ober sferik merged commit 6df3d85 into from
Steve Agalloco
Collaborator

:+1: nice work @paracycle

Ufuk Kayserilioglu
Collaborator

@sferik @spagalloco Thanks for all the help and for putting up with all my questions. It was a pleasure collaborating with you. I would love to contribute more in the future, please drop me a note if you think there is anything I can help with.

Erik Michaels-Ober
Owner

After this pull request, it would be my pleasure to add you as a collaborator on this project. Feel free to push any other changes you see fit.

Ufuk Kayserilioglu
Collaborator

@sferik Wow, I'd be thrilled. Thanks for the vote of confidence.

This was referenced
Joerg Sonnenberger jsonn referenced this pull request from a commit in jsonn/pkgsrc
tsutsui Update ruby-twitter to 5.5.1. Part of PR/48447.
Changelog (from CHANGELOG.md)

5.5.1
-----
* [Fix bug where `Twitter::Error::AlreadyFavorited` would never be raised](sferik/twitter#512) ([@polestarw](https://twitter.com/polestarw))
* [Fix bug where `Twitter::Error::AlreadyPosted` would never be raised](sferik/twitter@e6b37b9)
* [Restore `Twitter::Entities#entities?` as a public method](sferik/twitter@234a9e3)

5.5.0
-----
* [Add entities to `Twitter::DirectMessage`](sferik/twitter@d911deb)
* [Add conversion methods to `Twitter::NullObject`](sferik/twitter@4900fee)

5.4.1
-----
* [Default to maximum number of tweets per request](sferik/twitter@1e41b5d)

5.4.0
-----
* [Fix enumerable search interface](sferik/twitter@e14cc33)

5.3.1
-----
* [Add `Twitter::Utils` module](sferik/twitter@a1f47fb) ([@charliesome](https://twitter.com/charliesome))
* [Remove `Enumerable` monkey patch](sferik/twitter@818b28d) ([@charliesome](https://twitter.com/charliesome))
* [Don't spawning a new thread if there's only one element](sferik/twitter@c01ea83)
* [Introduce meaningful constant names](sferik/twitter@215c808) ([@futuresanta](https://twitter.com/futuresanta))
* [Automatically flatten `Twitter::Arguments`](sferik/twitter@a556028)

5.3.0
-----
* [Add `UNABLE_TO_VERIFY_CREDENTIALS` error code](sferik/twitter@6a47e71)
* [Don't suppress `Twitter::Error::Forbidden` in #follow and #follow!](sferik/twitter@b949c04)
* [Update memoizable dependency to ~> 0.3.1](sferik/twitter#501)

5.2.0
-----
* [Replace `URI` with `adressable`](sferik/twitter@7ea2f53)
* [Make `Twitter::Streaming::FriendList` an array](sferik/twitter@1a38e5e)
* [Add `Twitter::Streaming::DeletedTweet`](sferik/twitter@084025b)
* [Add `Twitter::Streaming::StallWarning`](sferik/twitter@b07ac50)
* [Add error code for "User is over daily status update limit"](sferik/twitter@76c088d)
* [`Twitter::Streaming::Client#site` can take a `String` or `Twitter::User`](sferik/twitter@e3ad4f2)
* [Update `http_parser.rb` dependency to `~> 0.6.0`](sferik/twitter@6d2f81b)

5.1.1
-----
* [Custom equalizer for `Twitter::Place`](sferik/twitter@79c76a9)

5.1.0
-----
* [Use `Addressable::URI` everywhere](sferik/twitter@97d7c68) ([@matthewrudy](https://twitter.com/matthewrudy))
* [Allow use of `Twitter::Place` instead of `place_id`](sferik/twitter@c2b31dd)
* [Allow use of `Twitter::Tweet` instead of `in_reply_to_status_id`](sferik/twitter@6b7d6c2)

5.0.1
-----
* [Fix `buftok` delimiter handling](sferik/twitter#484)
* [Started handling streaming deletes](sferik/twitter@8860b97)

5.0.0
-----
* [Remove `Twitter::API::Undocumented#status_activity` and `#statuses_activity`](sferik/twitter@7f97081)
* [Remove `Twitter::Tweet#favoriters`, `#repliers`, `#repliers_count`, and `#retweeters`](sferik/twitter@77cc963)
* [Remove identity map](sferik/twitter@ec7c2df)
* [Remove `Twitter::Cursor#all`](sferik/twitter@72be414)
* [Remove `Twitter::Cursor#collection`](sferik/twitter@9ae4621)
* [Remove `Twitter#from_user`](sferik/twitter@d2ae9f1)
* [Remove `ClientError`, `ServerError`, and `ParserError`](sferik/twitter@7284394)
* [Remove global configuration](sferik/twitter@239c5a8)
* [Remove ability to configure client with environment variables](sferik/twitter@17e9585)
* [Remove Brittish English aliases](sferik/twitter@572813b)
* [Replace `multi_json` with `json`](sferik/twitter@e5fc292)
* [Rename `oauth_token` to `access_token`](sferik/twitter@d360f80)
* [Move `Twitter::Arguments` out of `REST::API` namespace](sferik/twitter@8faa153)
* [Move `Twitter::Client` into `REST` namespace](sferik/twitter@5b8c3fd)
* [Add `Twitter::Streaming::Client`](sferik/twitter@23afe90)
* [Add `Twitter::Error::AlreadyPosted`](sferik/twitter@e11d2a2)
* [Add `Twitter::REST::Client#reverse_token`](sferik/twitter@39139c4)
* [Add `#url` methods to `Twitter::List`, `Twitter::Tweet`, and `Twitter::User`](sferik/twitter@a89ec0f)
* [Add `Twitter::Place#contained_within` and `#contained_within?`](sferik/twitter@23cc247)
* [Add `Twitter::GeoResults`](sferik/twitter@be1a0a1)
* [Add `NullObject`](sferik/twitter@17880f4)
* [Add predicate methods for any possible `NullObject`](sferik/twitter@eac5522)
* [Always return `URI` instead of `String`](sferik/twitter@341f68d)
* [Allow `URI` as argument](sferik/twitter@c207567)
* [Allow `String` in addition to `URI` objects](sferik/twitter@89a46fb)
* [Collection caching](sferik/twitter@d484d7d)
* [Implement `Twitter::Cursor#each` without making an extra HTTP request](sferik/twitter@8eeff57)
* [Make `Twitter::SearchResults` enumerable](sferik/twitter@d5ce853)
* [Make `Twitter::Base` objects immutable](sferik/twitter@69b1ef7)
* [Missing key now raises `KeyError`, not `ArgumentError`](sferik/twitter@f56698c)
* [Use `equalizer` instead of manually overwriting #==](sferik/twitter@a7ddf71)
* [Give methods more natural names](sferik/twitter@e593194)
* [Fix `Twitter::SearchResults#rpp` return value](sferik/twitter@28d7320)

4.8.1
-----
* [Ignore case of profile image extension](sferik/twitter@7376061)
* [Allow use of Twitter::Token in place of bearer token string](sferik/twitter@13596bc)
* [Add Twitter::API::Undocumented#tweet_count](sferik/twitter@795458a)
* [Add missing dependencies](sferik/twitter@e07e034) ([@tmatilai](https://twitter.com/tmatilai))

4.8.0
-----
* [Add `Twitter::SearchResults#refresh_url`](sferik/twitter@6bf08c0) ([@mustafaturan](https://twitter.com/mustafaturan))
* [Fix issue with wrong signature being generated when multipart data is posted](sferik/twitter@65ab90a) ([@mustafaturan](https://twitter.com/mustafaturan))
* [Restore compatibility with Ruby 1.8.7](sferik/twitter@fb63970)
* [Remove undocumented methods, retired in the APIpocalypse](sferik/twitter@cf6a91f)

4.7.0
-----
* [Add support for application-only authentication](sferik/twitter#387) ([@paracycle](https://twitter.com/paracycle))
* [Add support for `Twitter::Entity::Symbol` entities](sferik/twitter@a14a0cd) ([@anno](https://twitter.com/anno))
* [Add `Twitter::API::OAuth#invalidate_token`](sferik/twitter#372) ([@terenceponce](https://twitter.com/terenceponce))
* [Add `Twitter::API::Lists#lists_owned` method](sferik/twitter@9e97b51)
* [Add `Twitter::API::Tweets#retweeters_ids` method](sferik/twitter@8cf5b2d)
* [Add `Twitter::SearchResults#next_results`](sferik/twitter#365) ([@KentonWhite](https://twitter.com/KentonWhite))
* [Make consumer_key readable](sferik/twitter@a318869)
* [Loosen required_rubygems_version for compatibility with Ubuntu 10.04](sferik/twitter@41bd565)
* [Remove default SSL configuration options and override](sferik/twitter@113b14b)
9c16e32
Joerg Sonnenberger jsonn referenced this pull request from a commit in jsonn/pkgsrc
tsutsui Update ruby-twitter to 5.5.1. Part of PR/48447.
Changelog (from CHANGELOG.md)

5.5.1
-----
* [Fix bug where `Twitter::Error::AlreadyFavorited` would never be raised](sferik/twitter#512) ([@polestarw](https://twitter.com/polestarw))
* [Fix bug where `Twitter::Error::AlreadyPosted` would never be raised](sferik/twitter@e6b37b9)
* [Restore `Twitter::Entities#entities?` as a public method](sferik/twitter@234a9e3)

5.5.0
-----
* [Add entities to `Twitter::DirectMessage`](sferik/twitter@d911deb)
* [Add conversion methods to `Twitter::NullObject`](sferik/twitter@4900fee)

5.4.1
-----
* [Default to maximum number of tweets per request](sferik/twitter@1e41b5d)

5.4.0
-----
* [Fix enumerable search interface](sferik/twitter@e14cc33)

5.3.1
-----
* [Add `Twitter::Utils` module](sferik/twitter@a1f47fb) ([@charliesome](https://twitter.com/charliesome))
* [Remove `Enumerable` monkey patch](sferik/twitter@818b28d) ([@charliesome](https://twitter.com/charliesome))
* [Don't spawning a new thread if there's only one element](sferik/twitter@c01ea83)
* [Introduce meaningful constant names](sferik/twitter@215c808) ([@futuresanta](https://twitter.com/futuresanta))
* [Automatically flatten `Twitter::Arguments`](sferik/twitter@a556028)

5.3.0
-----
* [Add `UNABLE_TO_VERIFY_CREDENTIALS` error code](sferik/twitter@6a47e71)
* [Don't suppress `Twitter::Error::Forbidden` in #follow and #follow!](sferik/twitter@b949c04)
* [Update memoizable dependency to ~> 0.3.1](sferik/twitter#501)

5.2.0
-----
* [Replace `URI` with `adressable`](sferik/twitter@7ea2f53)
* [Make `Twitter::Streaming::FriendList` an array](sferik/twitter@1a38e5e)
* [Add `Twitter::Streaming::DeletedTweet`](sferik/twitter@084025b)
* [Add `Twitter::Streaming::StallWarning`](sferik/twitter@b07ac50)
* [Add error code for "User is over daily status update limit"](sferik/twitter@76c088d)
* [`Twitter::Streaming::Client#site` can take a `String` or `Twitter::User`](sferik/twitter@e3ad4f2)
* [Update `http_parser.rb` dependency to `~> 0.6.0`](sferik/twitter@6d2f81b)

5.1.1
-----
* [Custom equalizer for `Twitter::Place`](sferik/twitter@79c76a9)

5.1.0
-----
* [Use `Addressable::URI` everywhere](sferik/twitter@97d7c68) ([@matthewrudy](https://twitter.com/matthewrudy))
* [Allow use of `Twitter::Place` instead of `place_id`](sferik/twitter@c2b31dd)
* [Allow use of `Twitter::Tweet` instead of `in_reply_to_status_id`](sferik/twitter@6b7d6c2)

5.0.1
-----
* [Fix `buftok` delimiter handling](sferik/twitter#484)
* [Started handling streaming deletes](sferik/twitter@8860b97)

5.0.0
-----
* [Remove `Twitter::API::Undocumented#status_activity` and `#statuses_activity`](sferik/twitter@7f97081)
* [Remove `Twitter::Tweet#favoriters`, `#repliers`, `#repliers_count`, and `#retweeters`](sferik/twitter@77cc963)
* [Remove identity map](sferik/twitter@ec7c2df)
* [Remove `Twitter::Cursor#all`](sferik/twitter@72be414)
* [Remove `Twitter::Cursor#collection`](sferik/twitter@9ae4621)
* [Remove `Twitter#from_user`](sferik/twitter@d2ae9f1)
* [Remove `ClientError`, `ServerError`, and `ParserError`](sferik/twitter@7284394)
* [Remove global configuration](sferik/twitter@239c5a8)
* [Remove ability to configure client with environment variables](sferik/twitter@17e9585)
* [Remove Brittish English aliases](sferik/twitter@572813b)
* [Replace `multi_json` with `json`](sferik/twitter@e5fc292)
* [Rename `oauth_token` to `access_token`](sferik/twitter@d360f80)
* [Move `Twitter::Arguments` out of `REST::API` namespace](sferik/twitter@8faa153)
* [Move `Twitter::Client` into `REST` namespace](sferik/twitter@5b8c3fd)
* [Add `Twitter::Streaming::Client`](sferik/twitter@23afe90)
* [Add `Twitter::Error::AlreadyPosted`](sferik/twitter@e11d2a2)
* [Add `Twitter::REST::Client#reverse_token`](sferik/twitter@39139c4)
* [Add `#url` methods to `Twitter::List`, `Twitter::Tweet`, and `Twitter::User`](sferik/twitter@a89ec0f)
* [Add `Twitter::Place#contained_within` and `#contained_within?`](sferik/twitter@23cc247)
* [Add `Twitter::GeoResults`](sferik/twitter@be1a0a1)
* [Add `NullObject`](sferik/twitter@17880f4)
* [Add predicate methods for any possible `NullObject`](sferik/twitter@eac5522)
* [Always return `URI` instead of `String`](sferik/twitter@341f68d)
* [Allow `URI` as argument](sferik/twitter@c207567)
* [Allow `String` in addition to `URI` objects](sferik/twitter@89a46fb)
* [Collection caching](sferik/twitter@d484d7d)
* [Implement `Twitter::Cursor#each` without making an extra HTTP request](sferik/twitter@8eeff57)
* [Make `Twitter::SearchResults` enumerable](sferik/twitter@d5ce853)
* [Make `Twitter::Base` objects immutable](sferik/twitter@69b1ef7)
* [Missing key now raises `KeyError`, not `ArgumentError`](sferik/twitter@f56698c)
* [Use `equalizer` instead of manually overwriting #==](sferik/twitter@a7ddf71)
* [Give methods more natural names](sferik/twitter@e593194)
* [Fix `Twitter::SearchResults#rpp` return value](sferik/twitter@28d7320)

4.8.1
-----
* [Ignore case of profile image extension](sferik/twitter@7376061)
* [Allow use of Twitter::Token in place of bearer token string](sferik/twitter@13596bc)
* [Add Twitter::API::Undocumented#tweet_count](sferik/twitter@795458a)
* [Add missing dependencies](sferik/twitter@e07e034) ([@tmatilai](https://twitter.com/tmatilai))

4.8.0
-----
* [Add `Twitter::SearchResults#refresh_url`](sferik/twitter@6bf08c0) ([@mustafaturan](https://twitter.com/mustafaturan))
* [Fix issue with wrong signature being generated when multipart data is posted](sferik/twitter@65ab90a) ([@mustafaturan](https://twitter.com/mustafaturan))
* [Restore compatibility with Ruby 1.8.7](sferik/twitter@fb63970)
* [Remove undocumented methods, retired in the APIpocalypse](sferik/twitter@cf6a91f)

4.7.0
-----
* [Add support for application-only authentication](sferik/twitter#387) ([@paracycle](https://twitter.com/paracycle))
* [Add support for `Twitter::Entity::Symbol` entities](sferik/twitter@a14a0cd) ([@anno](https://twitter.com/anno))
* [Add `Twitter::API::OAuth#invalidate_token`](sferik/twitter#372) ([@terenceponce](https://twitter.com/terenceponce))
* [Add `Twitter::API::Lists#lists_owned` method](sferik/twitter@9e97b51)
* [Add `Twitter::API::Tweets#retweeters_ids` method](sferik/twitter@8cf5b2d)
* [Add `Twitter::SearchResults#next_results`](sferik/twitter#365) ([@KentonWhite](https://twitter.com/KentonWhite))
* [Make consumer_key readable](sferik/twitter@a318869)
* [Loosen required_rubygems_version for compatibility with Ubuntu 10.04](sferik/twitter@41bd565)
* [Remove default SSL configuration options and override](sferik/twitter@113b14b)
147f147
Joerg Sonnenberger jsonn referenced this pull request from a commit in jsonn/pkgsrc
tsutsui Update ruby-twitter to 5.5.1. Part of PR/48447.
Changelog (from CHANGELOG.md)

5.5.1
-----
* [Fix bug where `Twitter::Error::AlreadyFavorited` would never be raised](sferik/twitter#512) ([@polestarw](https://twitter.com/polestarw))
* [Fix bug where `Twitter::Error::AlreadyPosted` would never be raised](sferik/twitter@e6b37b9)
* [Restore `Twitter::Entities#entities?` as a public method](sferik/twitter@234a9e3)

5.5.0
-----
* [Add entities to `Twitter::DirectMessage`](sferik/twitter@d911deb)
* [Add conversion methods to `Twitter::NullObject`](sferik/twitter@4900fee)

5.4.1
-----
* [Default to maximum number of tweets per request](sferik/twitter@1e41b5d)

5.4.0
-----
* [Fix enumerable search interface](sferik/twitter@e14cc33)

5.3.1
-----
* [Add `Twitter::Utils` module](sferik/twitter@a1f47fb) ([@charliesome](https://twitter.com/charliesome))
* [Remove `Enumerable` monkey patch](sferik/twitter@818b28d) ([@charliesome](https://twitter.com/charliesome))
* [Don't spawning a new thread if there's only one element](sferik/twitter@c01ea83)
* [Introduce meaningful constant names](sferik/twitter@215c808) ([@futuresanta](https://twitter.com/futuresanta))
* [Automatically flatten `Twitter::Arguments`](sferik/twitter@a556028)

5.3.0
-----
* [Add `UNABLE_TO_VERIFY_CREDENTIALS` error code](sferik/twitter@6a47e71)
* [Don't suppress `Twitter::Error::Forbidden` in #follow and #follow!](sferik/twitter@b949c04)
* [Update memoizable dependency to ~> 0.3.1](sferik/twitter#501)

5.2.0
-----
* [Replace `URI` with `adressable`](sferik/twitter@7ea2f53)
* [Make `Twitter::Streaming::FriendList` an array](sferik/twitter@1a38e5e)
* [Add `Twitter::Streaming::DeletedTweet`](sferik/twitter@084025b)
* [Add `Twitter::Streaming::StallWarning`](sferik/twitter@b07ac50)
* [Add error code for "User is over daily status update limit"](sferik/twitter@76c088d)
* [`Twitter::Streaming::Client#site` can take a `String` or `Twitter::User`](sferik/twitter@e3ad4f2)
* [Update `http_parser.rb` dependency to `~> 0.6.0`](sferik/twitter@6d2f81b)

5.1.1
-----
* [Custom equalizer for `Twitter::Place`](sferik/twitter@79c76a9)

5.1.0
-----
* [Use `Addressable::URI` everywhere](sferik/twitter@97d7c68) ([@matthewrudy](https://twitter.com/matthewrudy))
* [Allow use of `Twitter::Place` instead of `place_id`](sferik/twitter@c2b31dd)
* [Allow use of `Twitter::Tweet` instead of `in_reply_to_status_id`](sferik/twitter@6b7d6c2)

5.0.1
-----
* [Fix `buftok` delimiter handling](sferik/twitter#484)
* [Started handling streaming deletes](sferik/twitter@8860b97)

5.0.0
-----
* [Remove `Twitter::API::Undocumented#status_activity` and `#statuses_activity`](sferik/twitter@7f97081)
* [Remove `Twitter::Tweet#favoriters`, `#repliers`, `#repliers_count`, and `#retweeters`](sferik/twitter@77cc963)
* [Remove identity map](sferik/twitter@ec7c2df)
* [Remove `Twitter::Cursor#all`](sferik/twitter@72be414)
* [Remove `Twitter::Cursor#collection`](sferik/twitter@9ae4621)
* [Remove `Twitter#from_user`](sferik/twitter@d2ae9f1)
* [Remove `ClientError`, `ServerError`, and `ParserError`](sferik/twitter@7284394)
* [Remove global configuration](sferik/twitter@239c5a8)
* [Remove ability to configure client with environment variables](sferik/twitter@17e9585)
* [Remove Brittish English aliases](sferik/twitter@572813b)
* [Replace `multi_json` with `json`](sferik/twitter@e5fc292)
* [Rename `oauth_token` to `access_token`](sferik/twitter@d360f80)
* [Move `Twitter::Arguments` out of `REST::API` namespace](sferik/twitter@8faa153)
* [Move `Twitter::Client` into `REST` namespace](sferik/twitter@5b8c3fd)
* [Add `Twitter::Streaming::Client`](sferik/twitter@23afe90)
* [Add `Twitter::Error::AlreadyPosted`](sferik/twitter@e11d2a2)
* [Add `Twitter::REST::Client#reverse_token`](sferik/twitter@39139c4)
* [Add `#url` methods to `Twitter::List`, `Twitter::Tweet`, and `Twitter::User`](sferik/twitter@a89ec0f)
* [Add `Twitter::Place#contained_within` and `#contained_within?`](sferik/twitter@23cc247)
* [Add `Twitter::GeoResults`](sferik/twitter@be1a0a1)
* [Add `NullObject`](sferik/twitter@17880f4)
* [Add predicate methods for any possible `NullObject`](sferik/twitter@eac5522)
* [Always return `URI` instead of `String`](sferik/twitter@341f68d)
* [Allow `URI` as argument](sferik/twitter@c207567)
* [Allow `String` in addition to `URI` objects](sferik/twitter@89a46fb)
* [Collection caching](sferik/twitter@d484d7d)
* [Implement `Twitter::Cursor#each` without making an extra HTTP request](sferik/twitter@8eeff57)
* [Make `Twitter::SearchResults` enumerable](sferik/twitter@d5ce853)
* [Make `Twitter::Base` objects immutable](sferik/twitter@69b1ef7)
* [Missing key now raises `KeyError`, not `ArgumentError`](sferik/twitter@f56698c)
* [Use `equalizer` instead of manually overwriting #==](sferik/twitter@a7ddf71)
* [Give methods more natural names](sferik/twitter@e593194)
* [Fix `Twitter::SearchResults#rpp` return value](sferik/twitter@28d7320)

4.8.1
-----
* [Ignore case of profile image extension](sferik/twitter@7376061)
* [Allow use of Twitter::Token in place of bearer token string](sferik/twitter@13596bc)
* [Add Twitter::API::Undocumented#tweet_count](sferik/twitter@795458a)
* [Add missing dependencies](sferik/twitter@e07e034) ([@tmatilai](https://twitter.com/tmatilai))

4.8.0
-----
* [Add `Twitter::SearchResults#refresh_url`](sferik/twitter@6bf08c0) ([@mustafaturan](https://twitter.com/mustafaturan))
* [Fix issue with wrong signature being generated when multipart data is posted](sferik/twitter@65ab90a) ([@mustafaturan](https://twitter.com/mustafaturan))
* [Restore compatibility with Ruby 1.8.7](sferik/twitter@fb63970)
* [Remove undocumented methods, retired in the APIpocalypse](sferik/twitter@cf6a91f)

4.7.0
-----
* [Add support for application-only authentication](sferik/twitter#387) ([@paracycle](https://twitter.com/paracycle))
* [Add support for `Twitter::Entity::Symbol` entities](sferik/twitter@a14a0cd) ([@anno](https://twitter.com/anno))
* [Add `Twitter::API::OAuth#invalidate_token`](sferik/twitter#372) ([@terenceponce](https://twitter.com/terenceponce))
* [Add `Twitter::API::Lists#lists_owned` method](sferik/twitter@9e97b51)
* [Add `Twitter::API::Tweets#retweeters_ids` method](sferik/twitter@8cf5b2d)
* [Add `Twitter::SearchResults#next_results`](sferik/twitter#365) ([@KentonWhite](https://twitter.com/KentonWhite))
* [Make consumer_key readable](sferik/twitter@a318869)
* [Loosen required_rubygems_version for compatibility with Ubuntu 10.04](sferik/twitter@41bd565)
* [Remove default SSL configuration options and override](sferik/twitter@113b14b)
3288e29
Joerg Sonnenberger jsonn referenced this pull request from a commit in jsonn/pkgsrc
tsutsui Update ruby-twitter to 5.5.1. Part of PR/48447.
Changelog (from CHANGELOG.md)

5.5.1
-----
* [Fix bug where `Twitter::Error::AlreadyFavorited` would never be raised](sferik/twitter#512) ([@polestarw](https://twitter.com/polestarw))
* [Fix bug where `Twitter::Error::AlreadyPosted` would never be raised](sferik/twitter@e6b37b9)
* [Restore `Twitter::Entities#entities?` as a public method](sferik/twitter@234a9e3)

5.5.0
-----
* [Add entities to `Twitter::DirectMessage`](sferik/twitter@d911deb)
* [Add conversion methods to `Twitter::NullObject`](sferik/twitter@4900fee)

5.4.1
-----
* [Default to maximum number of tweets per request](sferik/twitter@1e41b5d)

5.4.0
-----
* [Fix enumerable search interface](sferik/twitter@e14cc33)

5.3.1
-----
* [Add `Twitter::Utils` module](sferik/twitter@a1f47fb) ([@charliesome](https://twitter.com/charliesome))
* [Remove `Enumerable` monkey patch](sferik/twitter@818b28d) ([@charliesome](https://twitter.com/charliesome))
* [Don't spawning a new thread if there's only one element](sferik/twitter@c01ea83)
* [Introduce meaningful constant names](sferik/twitter@215c808) ([@futuresanta](https://twitter.com/futuresanta))
* [Automatically flatten `Twitter::Arguments`](sferik/twitter@a556028)

5.3.0
-----
* [Add `UNABLE_TO_VERIFY_CREDENTIALS` error code](sferik/twitter@6a47e71)
* [Don't suppress `Twitter::Error::Forbidden` in #follow and #follow!](sferik/twitter@b949c04)
* [Update memoizable dependency to ~> 0.3.1](sferik/twitter#501)

5.2.0
-----
* [Replace `URI` with `adressable`](sferik/twitter@7ea2f53)
* [Make `Twitter::Streaming::FriendList` an array](sferik/twitter@1a38e5e)
* [Add `Twitter::Streaming::DeletedTweet`](sferik/twitter@084025b)
* [Add `Twitter::Streaming::StallWarning`](sferik/twitter@b07ac50)
* [Add error code for "User is over daily status update limit"](sferik/twitter@76c088d)
* [`Twitter::Streaming::Client#site` can take a `String` or `Twitter::User`](sferik/twitter@e3ad4f2)
* [Update `http_parser.rb` dependency to `~> 0.6.0`](sferik/twitter@6d2f81b)

5.1.1
-----
* [Custom equalizer for `Twitter::Place`](sferik/twitter@79c76a9)

5.1.0
-----
* [Use `Addressable::URI` everywhere](sferik/twitter@97d7c68) ([@matthewrudy](https://twitter.com/matthewrudy))
* [Allow use of `Twitter::Place` instead of `place_id`](sferik/twitter@c2b31dd)
* [Allow use of `Twitter::Tweet` instead of `in_reply_to_status_id`](sferik/twitter@6b7d6c2)

5.0.1
-----
* [Fix `buftok` delimiter handling](sferik/twitter#484)
* [Started handling streaming deletes](sferik/twitter@8860b97)

5.0.0
-----
* [Remove `Twitter::API::Undocumented#status_activity` and `#statuses_activity`](sferik/twitter@7f97081)
* [Remove `Twitter::Tweet#favoriters`, `#repliers`, `#repliers_count`, and `#retweeters`](sferik/twitter@77cc963)
* [Remove identity map](sferik/twitter@ec7c2df)
* [Remove `Twitter::Cursor#all`](sferik/twitter@72be414)
* [Remove `Twitter::Cursor#collection`](sferik/twitter@9ae4621)
* [Remove `Twitter#from_user`](sferik/twitter@d2ae9f1)
* [Remove `ClientError`, `ServerError`, and `ParserError`](sferik/twitter@7284394)
* [Remove global configuration](sferik/twitter@239c5a8)
* [Remove ability to configure client with environment variables](sferik/twitter@17e9585)
* [Remove Brittish English aliases](sferik/twitter@572813b)
* [Replace `multi_json` with `json`](sferik/twitter@e5fc292)
* [Rename `oauth_token` to `access_token`](sferik/twitter@d360f80)
* [Move `Twitter::Arguments` out of `REST::API` namespace](sferik/twitter@8faa153)
* [Move `Twitter::Client` into `REST` namespace](sferik/twitter@5b8c3fd)
* [Add `Twitter::Streaming::Client`](sferik/twitter@23afe90)
* [Add `Twitter::Error::AlreadyPosted`](sferik/twitter@e11d2a2)
* [Add `Twitter::REST::Client#reverse_token`](sferik/twitter@39139c4)
* [Add `#url` methods to `Twitter::List`, `Twitter::Tweet`, and `Twitter::User`](sferik/twitter@a89ec0f)
* [Add `Twitter::Place#contained_within` and `#contained_within?`](sferik/twitter@23cc247)
* [Add `Twitter::GeoResults`](sferik/twitter@be1a0a1)
* [Add `NullObject`](sferik/twitter@17880f4)
* [Add predicate methods for any possible `NullObject`](sferik/twitter@eac5522)
* [Always return `URI` instead of `String`](sferik/twitter@341f68d)
* [Allow `URI` as argument](sferik/twitter@c207567)
* [Allow `String` in addition to `URI` objects](sferik/twitter@89a46fb)
* [Collection caching](sferik/twitter@d484d7d)
* [Implement `Twitter::Cursor#each` without making an extra HTTP request](sferik/twitter@8eeff57)
* [Make `Twitter::SearchResults` enumerable](sferik/twitter@d5ce853)
* [Make `Twitter::Base` objects immutable](sferik/twitter@69b1ef7)
* [Missing key now raises `KeyError`, not `ArgumentError`](sferik/twitter@f56698c)
* [Use `equalizer` instead of manually overwriting #==](sferik/twitter@a7ddf71)
* [Give methods more natural names](sferik/twitter@e593194)
* [Fix `Twitter::SearchResults#rpp` return value](sferik/twitter@28d7320)

4.8.1
-----
* [Ignore case of profile image extension](sferik/twitter@7376061)
* [Allow use of Twitter::Token in place of bearer token string](sferik/twitter@13596bc)
* [Add Twitter::API::Undocumented#tweet_count](sferik/twitter@795458a)
* [Add missing dependencies](sferik/twitter@e07e034) ([@tmatilai](https://twitter.com/tmatilai))

4.8.0
-----
* [Add `Twitter::SearchResults#refresh_url`](sferik/twitter@6bf08c0) ([@mustafaturan](https://twitter.com/mustafaturan))
* [Fix issue with wrong signature being generated when multipart data is posted](sferik/twitter@65ab90a) ([@mustafaturan](https://twitter.com/mustafaturan))
* [Restore compatibility with Ruby 1.8.7](sferik/twitter@fb63970)
* [Remove undocumented methods, retired in the APIpocalypse](sferik/twitter@cf6a91f)

4.7.0
-----
* [Add support for application-only authentication](sferik/twitter#387) ([@paracycle](https://twitter.com/paracycle))
* [Add support for `Twitter::Entity::Symbol` entities](sferik/twitter@a14a0cd) ([@anno](https://twitter.com/anno))
* [Add `Twitter::API::OAuth#invalidate_token`](sferik/twitter#372) ([@terenceponce](https://twitter.com/terenceponce))
* [Add `Twitter::API::Lists#lists_owned` method](sferik/twitter@9e97b51)
* [Add `Twitter::API::Tweets#retweeters_ids` method](sferik/twitter@8cf5b2d)
* [Add `Twitter::SearchResults#next_results`](sferik/twitter#365) ([@KentonWhite](https://twitter.com/KentonWhite))
* [Make consumer_key readable](sferik/twitter@a318869)
* [Loosen required_rubygems_version for compatibility with Ubuntu 10.04](sferik/twitter@41bd565)
* [Remove default SSL configuration options and override](sferik/twitter@113b14b)
ca6231f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 16, 2013
  1. Ufuk Kayserilioglu

    Adding Bearer Token support

    paracycle authored
    - Added :bearer_token to the list of configuration parameters
    - The default value of the bearer_token is read from the environment
      variable
    - Requests send the "Bearer AAA..." type authentication header if a
      bearer token exists
    - Added tests to verify that bearer token implementation works as
      intended
  2. Ufuk Kayserilioglu

    Adding oath2/token method

    paracycle authored
    - I had to replicate the functionality of the request method to
      accomodate for the abnormal way we have to make the request
    - Configurable can supply the base64 encoded bearer token credential
Commits on Apr 17, 2013
  1. Ufuk Kayserilioglu
  2. Ufuk Kayserilioglu
Commits on Apr 18, 2013
  1. Ufuk Kayserilioglu

    Unifying the request method of Twitter::Client

    paracycle authored
    - The request method is using a helper method request_setup that returns
      a Proc which sets up the correct headers for the current request.
    - Each type of different authentication header is being returned by a
      separate private method and they all have tests.
  2. Ufuk Kayserilioglu
  3. Ufuk Kayserilioglu

    Removing left over code.

    paracycle authored
  4. Ufuk Kayserilioglu

    Unindented 'end' statement

    paracycle authored
  5. Ufuk Kayserilioglu
This page is out of date. Refresh to see the latest.
19 lib/twitter/api/oauth.rb
View
@@ -6,6 +6,25 @@ 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, :post, "/oauth2/token", :grant_type => "client_credentials", :bearer_token_request => true)
+ end
+ alias bearer_token token
+
# 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
44 lib/twitter/client.rb
View
@@ -20,6 +20,7 @@
require 'twitter/error/client_error'
require 'twitter/error/decode_error'
require 'simple_oauth'
+require 'base64'
require 'uri'
module Twitter
@@ -78,11 +79,33 @@ def put(path, params={})
end
private
+ # Returns a proc that can be used to setup the Faraday::Request headers
+ #
+ # @param method [Symbol]
+ # @param path [String]
+ # @param params [Hash]
+ # @return [Proc]
+ def request_setup(method, path, params)
+ if params.delete :bearer_token_request
+ Proc.new 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] = '*/*' # It is important we set this, otherwise we get an error.
+ end
+ elsif application_only_auth?
+ Proc.new do |request|
+ request.headers[:authorization] = bearer_auth_header
+ end
+ else
+ Proc.new do |request|
+ request.headers[:authorization] = oauth_auth_header(method, path, params).to_s
+ end
+ end
+ end
def request(method, path, params={}, signature_params=params)
- connection.send(method.to_sym, path, params) do |request|
- request.headers[:authorization] = auth_header(method.to_sym, path, signature_params).to_s
- end.env
+ request_setup = request_setup(method, path, params)
+ connection.send(method.to_sym, path, params, &request_setup).env
rescue Faraday::Error::ClientError
raise Twitter::Error::ClientError
rescue MultiJson::DecodeError
@@ -100,10 +123,21 @@ def connection
end
end
- def auth_header(method, path, params={})
+ # Generates authentication header for a bearer token request
+ #
+ # @return [String]
+ def bearer_token_credentials_auth_header
+ basic_auth_token = Base64.strict_encode64("#{@consumer_key}:#{@consumer_secret}")
+ "Basic #{basic_auth_token}"
+ end
+
+ def bearer_auth_header
+ "Bearer #{@bearer_token}"
+ end
+
+ def oauth_auth_header(method, path, params={})
uri = URI(@endpoint + path)
SimpleOAuth::Header.new(method, uri, params, credentials)
end
-
end
end
8 lib/twitter/configurable.rb
View
@@ -4,7 +4,7 @@
module Twitter
module Configurable
extend Forwardable
- attr_writer :consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret
+ attr_writer :consumer_key, :consumer_secret, :oauth_token, :oauth_token_secret, :bearer_token
attr_accessor :endpoint, :connection_options, :identity_map, :middleware
def_delegator :options, :hash
@@ -16,6 +16,7 @@ def keys
:consumer_secret,
:oauth_token,
:oauth_token_secret,
+ :bearer_token,
:endpoint,
:connection_options,
:identity_map,
@@ -37,7 +38,7 @@ def configure
# @return [Boolean]
def credentials?
- credentials.values.all?
+ credentials.values.all? || @bearer_token
end
def reset!
@@ -49,6 +50,9 @@ def reset!
alias setup reset!
private
+ def application_only_auth?
+ !!@bearer_token
+ end
# @return [Hash]
def credentials
5 lib/twitter/default.rb
View
@@ -69,6 +69,11 @@ def oauth_token_secret
ENV['TWITTER_OAUTH_TOKEN_SECRET']
end
+ # @return [String]
+ def bearer_token
+ ENV['TWITTER_BEARER_TOKEN']
+ end
+
# @note This is configurable in case you want to use a Twitter-compatible endpoint.
# @see http://status.net/wiki/Twitter-compatible_API
# @see http://en.blog.wordpress.com/2009/12/12/twitter-api/
26 spec/twitter/api/oauth_spec.rb
View
@@ -3,7 +3,31 @@
describe Twitter::API::OAuth do
before do
- @client = Twitter::Client.new
+ @client = Twitter::Client.new :consumer_key => 'CK', :consumer_secret => 'CS'
+ end
+
+ describe "#token" do
+ before do
+ # WebMock treats Basic Auth differently so we have to chack against the full url with credentials.
+ @oauth2_token_url = "https://CK:CS@api.twitter.com/oauth2/token"
+ stub_request(:post, @oauth2_token_url).with(:body => "grant_type=client_credentials").to_return(:body => '{"token_type" : "bearer", "access_token" : "AAAA%2FAAA%3DAAAAAAAA"}', :headers => {:content_type => "application/json; charset=utf-8"})
+ end
+ it "requests the correct resource" do
+ @client.token
+ expect(a_request(:post, @oauth2_token_url).with(:body => {:grant_type => "client_credentials"})).to have_been_made
+ end
+ it "requests with the correct headers" do
+ @client.token
+ expect(a_request(:post, @oauth2_token_url).with(:headers => {
+ :content_type => "application/x-www-form-urlencoded; charset=UTF-8",
+ :accept => "*/*"
+ })).to have_been_made
+ end
+ it "returns the bearer token" do
+ token = @client.token
+ expect(token.access_token).to eq "AAAA%2FAAA%3DAAAAAAAA"
+ expect(token.token_type).to eq "bearer"
+ end
end
describe "#invalidate_token" do
24 spec/twitter/client_spec.rb
View
@@ -38,6 +38,7 @@
:middleware => Proc.new{},
:oauth_token => 'OT',
:oauth_token_secret => 'OS',
+ :bearer_token => 'BT',
:identity_map => ::Hash
}
end
@@ -139,10 +140,10 @@
end
end
- describe "#auth_header" do
+ describe "#oauth_auth_header" do
it "creates the correct auth headers" do
uri = "/1.1/direct_messages.json"
- authorization = subject.send(:auth_header, :get, uri)
+ authorization = subject.send(:oauth_auth_header, :get, uri)
expect(authorization.options[:signature_method]).to eq "HMAC-SHA1"
expect(authorization.options[:version]).to eq "1.0"
expect(authorization.options[:consumer_key]).to eq "CK"
@@ -152,4 +153,23 @@
end
end
+ describe "#bearer_auth_header" do
+ subject do
+ Twitter::Client.new(:bearer_token => "BT")
+ end
+
+ it "creates the correct auth headers with supplied bearer_token" do
+ uri = "/1.1/direct_messages.json"
+ authorization = subject.send(:bearer_auth_header)
+ expect(authorization).to eq "Bearer BT"
+ end
+ end
+
+ describe "#bearer_token_credentials_auth_header" do
+ it "creates the correct auth header with supplied consumer_key and consumer_secret" do
+ uri = "/1.1/direct_messages.json"
+ authorization = subject.send(:bearer_token_credentials_auth_header)
+ expect(authorization).to eq "Basic #{Base64.strict_encode64("CK:CS")}"
+ end
+ end
end
8 spec/twitter_spec.rb
View
@@ -91,7 +91,13 @@
end
describe ".credentials?" do
- it "returns true if all credentials are present" do
+ it "returns true if only bearer_token is supplied" do
+ Twitter.configure do |config|
+ config.bearer_token = 'BT'
+ end
+ expect(Twitter.credentials?).to be_true
+ end
+ it "returns true if all oauth credentials are present" do
Twitter.configure do |config|
config.consumer_key = 'CK'
config.consumer_secret = 'CS'
Something went wrong with that request. Please try again.