2
votes

I'm developing a web app that will let users tweet posts and links, but I can't seem to get Twitter and Omniauth to play nicely together. I'm currently running on Rails 3.0.6 and Ruby 1.8.7, with the Twitter gem 1.4.1 and Omniauth gem 0.2.5

I can authenticate the users fine, but when it comes to sending a tweet, I'm just given the error:

 POST https://api.twitter.com/1/statuses/update.json: 401: Incorrect signature

I followed this tutorial, and have placed my consumer key and consumer secret in a Twitter configure block in my Omniauth initializer, but not the oauth token or oauth secret because these will surely be used on a per-user basis.

omniauth.rb

 Twitter.configure do |config|
   config.consumer_key = "XXXXXXXXXXXXXXXXXXXXXX"
   config.consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
 end

user.rb

  def twitter
     unless @twitter_user
       provider = self.authentications.find_by_provider('twitter')
       @twitter_user = Twitter::Client.new(:oauth_token => provider.token, :oauth_token_secret => provider.secret) rescue nil
     end
     @twitter_user
  end

I then form the request using:

 current_user.twitter.update("Hello World!")

And that's what then gives me the 401 error.

Any ideas? Thanks!

2

2 Answers

1
votes

Your user.rb code is using the wrong format. They've changed quite a lot. You need something like this now:

require 'twitter'
class TwitterToken < ConsumerToken
  TWITTER_SETTINGS={:site=>"http://api.twitter.com", :request_endpoint => 'http://api.twitter.com',}
  def self.consumer
    @consumer||=OAuth::Consumer.new credentials[:key],credentials[:secret],TWITTER_SETTINGS
  end

  def client
    Twitter.configure do |config|
      config.consumer_key = TwitterToken.consumer.key
      config.consumer_secret = TwitterToken.consumer.secret
      config.oauth_token = token
      config.oauth_token_secret = secret
    end
    @client ||= Twitter::Client.new
  end
end
0
votes

I was having similar problems with that version of OmniAuth, I moved back to version 0.2.0 and all the 401's stopped happening.