A little tip, as this took some hunting around.
So you’ve added Twitter login via Devise. How do you then make signed Twitter calls on behalf of authenticated users?
UPDATE: Made a little module for this, at the end of this post.
Your Devise config (initializers/devise.rb) looks like this:
- config.omniauth :twitter, 'this-is-my-consumer-id', 'this-is-my-consumer-secret'
To make a Twitter call signed by the user, you’ll need to access that config data. Here’s how:
- twitter_config = Devise.omniauth_configs[:twitter]
- consumer_key = twitter_config.strategy.consumer_key
- consumer_secret = twitter_config.strategy.consumer_secret
Now using twitter-oauth gem, you can make a call like so:
- t = User.find('joe').user_tokens.where(provider: twitter)
- twitter = TwitterOAuth::Client.new consumer_key: consumer_key, consumer_secret: consumer_secret, token: t.token, secret: t.secret
- twitter.search 'groo'
You can find the full list of Twitter OAuth API calls in the source.
Here’s a module that does this, which you can mix into User models: