Skip to content

Instantly share code, notes, and snippets.

@rwz
Created June 14, 2011 10:43
Show Gist options
  • Save rwz/1024662 to your computer and use it in GitHub Desktop.
Save rwz/1024662 to your computer and use it in GitHub Desktop.
Twitter authentication fail with Omniauth credentials [FIXED, see https://github.com/jnunemaker/twitter/issues/171]
class Authentication < ActiveRecord::Base
belongs_to :user
serialize :info
serialize :credentials
def update_from_omniauth(omniauth)
self.uid = omniauth['uid']
self.provider = omniauth['provider']
self.info = omniauth['user_info']
self.credentials = omniauth['credentials']
end
def update_from_omniauth!(omniauth)
update_from_omniauth(omniauth)
save!
end
end
Twitter.configure do |config|
config.consumer_key = 'MYCONSUMERKEY'
config.consumer_secret = 'MYCONSUMERKEYSECRET'
end
user = User.last
user.authentications.find_by_provider('twitter').credentials
=> {"token"=>"OAUTHTOKEN", "secret"=>"OAUTHSECRET"}
twitter = user.twitter
=> #<Twitter::Client:0x000001064644f0 @adapter=:net_http, @consumer_key="MYCONSUMERKEY", @consumer_secret="MYCONSUMERKEYSECRET", @endpoint="https://api.twitter.com/1/", @format=:json, @gateway=nil, @oauth_token="OAUTHTOKEN", @oauth_token_secret="OAUTHSECRET", @proxy=nil, @search_endpoint="https://search.twitter.com/", @user_agent="Twitter Ruby Gem 1.5.0">
twitter.friends
=> # lots of stuff
twitter.update('test')
Twitter::Unauthorized: POST https://api.twitter.com/1/statuses/update.json: 401: Could not authenticate with OAuth.
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/twitter-1.5.0/lib/faraday/response/raise_http_4xx.rb:12:in `on_complete'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/faraday-0.6.1/lib/faraday/response.rb:9:in `block in call'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/faraday-0.6.1/lib/faraday/response.rb:62:in `on_complete'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/faraday-0.6.1/lib/faraday/response.rb:8:in `call'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/faraday-0.6.1/lib/faraday/request/url_encoded.rb:14:in `call'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/faraday-0.6.1/lib/faraday/request/multipart.rb:13:in `call'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/faraday_middleware-0.6.4/lib/faraday/request/oauth.rb:16:in `call'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/twitter-1.5.0/lib/faraday/request/multipart_with_file.rb:16:in `call'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/faraday-0.6.1/lib/faraday/request.rb:88:in `run'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/faraday-0.6.1/lib/faraday/request.rb:28:in `run'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/faraday-0.6.1/lib/faraday/connection.rb:170:in `run_request'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/faraday-0.6.1/lib/faraday/connection.rb:69:in `post'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/twitter-1.5.0/lib/twitter/request.rb:28:in `request'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/twitter-1.5.0/lib/twitter/request.rb:11:in `post'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/twitter-1.5.0/lib/twitter/client/tweets.rb:43:in `update'
from (irb):9
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:45:in `start'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:8:in `start'
from /Users/rwz/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'>>
# got it fixed by downgrading faraday_middleware to 0.6.3
has_many :authentications
def twitter
@twitter ||= begin
auth = authentications.find_by_provider('twitter')
auth && ::Twitter::Client.new(
:oauth_token => auth.credentials['token'],
:oauth_token_secret => auth.credentials['secret'],
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment