Calling Twitter When Using Devise

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:

  1. 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:

  1. twitter_config = Devise.omniauth_configs[:twitter]
  2. consumer_key = twitter_config.strategy.consumer_key
  3. consumer_secret = twitter_config.strategy.consumer_secret

Now using twitter-oauth gem, you can make a call like so:

  1. t = User.find('joe').user_tokens.where(provider: twitter)
  2. twitter = consumer_key: consumer_key, consumer_secret: consumer_secret, token: t.token, secret: t.secret
  3. '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:

2 thoughts on Calling Twitter When Using Devise

Leave a Reply