Keep getting OAuth::Unauthorized error when using oauth and twitter ruby gems
This was one of the most annoying things to debug that I have come across. I was outputting in a couple places by accident because the URL's are dynamic and they happened to not be defined in my test case (i use this to display chart data and there is not enough right now so the google chart api URL's are blank). This caused my browser to make multiple requests to my localhost when some pages were loaded. Somehow that made the oauth process crap out. Obviously there is no way for people on S.O. to know about my application specific issue so I had to answer my own question.
OAuth error in using twitter API v2 for posting a tweet
You need to insert
require 'oauth/request_proxy/typhoeus_request'
and your code may complete task you desire.
Other lines looks good to me!
In oauth/request_proxy.rb
, oauth library check class of request object.
https://github.com/oauth-xx/oauth-ruby/blob/master/lib/oauth/request_proxy.rb
return request if request.is_a?(OAuth::RequestProxy::Base)
klass = available_proxies[request.class]
# Search for possible superclass matches.
if klass.nil?
request_parent = available_proxies.keys.find { |rc| request.is_a?(rc) }
klass = available_proxies[request_parent]
end
raise UnknownRequestType, request.class.to_s unless klass
By requiring 'oauth/request_proxy/typhoeus_request'
, Typhoeus::Request
inherits OAuth::RequestProxy::Base
and raising UnknownRequestType error can be avoided.
https://github.com/oauth-xx/oauth-ruby/blob/master/lib/oauth/request_proxy/typhoeus_request.rb
Rails: OAuth::Unauthorized 401 Authorization Required using OmniAuth-Twitter
After a night of tearing my hair out, I took at look at the callback URL on Twitter developer console.
Save yourselves some trouble and don't forget to set this. It's not mentioned directly in the RailsCast, although Ryan does briefly pass over it.
When you set the callback URL, don't just put //localhost:3000
it won't work. Instead use:
http://127.0.0.1:3000/
401 unauthorized from Twitter API with oauth gem in Ruby
On this line, you should replace "APIKey" and "APISecret" with what you pulled from the CONSUMER_* environment variables.
consumer = OAuth::Consumer.new("APIKey", "APISecret", { :site => "https://api.twitter.com", :scheme => :header })
The example code from Twitter works fine for me. The wrong consumer keys will give you 401 for sure.
Twitter gem authentication error (using omniauth-twitter token/secret)
I finally identified the problem (after a night of sleep) - a silly configuration issue!
The Twitter gem picked up consumer key/secret pair from the twitter.rb initializer file while omniauth-twitter got it's pair through the environment (sorry, did not show this essential information in my question because that part 'worked').
I mixed up Twitter accounts and utilised two different Twitter applications on the consumer key part. Twitter and omniauth-twitter gems were on different applications. This approach doesn't (and shouldn't) work and causes authentication failure. I should have kept the consumer details DRY of course :)
omniauth OAuthException & OAuth::Unauthorized
Alex D. is correct in that the ENV[] breaks it. To create omniauth.rb so that it uses different keys in different environments just put:
provider :twitter, TWITTER_KEY, TWITTER_SECRET
in omniauth.rb
and then in your environment config files (config/environments/development.rb, etc.) put the key you want to use for that environment.
config/environments/development.rb:
TWITTER_KEY = 'aaaaaaa'
TWITTER_SECRET = 'aaaabbbbbb'
config/environments/production.rb:
TWITTER_KEY = 'ccccccc'
TWITTER_SECRET = 'ccccdddddd'
Related Topics
Order Products by Association Count
Handling Exceptions Raised in a Ruby Thread
How to Get a Particular Line from a File
How to Change Column Type in Heroku
How to Find Out Who Is Connected to Actioncable
Ruby Block to String Instead of Executing
How to Create a Nokogiri Case Insensitive Xpath Selector
How to Read a File from Bottom to Top in Ruby
Xpath Expression for Regex-Like Matching
How to Run a Single Test/Spec File in Rspec
How to Install Ruby 2.1.4 on Ubuntu 14.04
Ruby Design Pattern: How to Make an Extensible Factory Class
How to Elegantly Rename All Keys in a Hash in Ruby
Rails - Redirecting Console Output to a File
How to Dump an Object's Fields to the Console
How to Get the Last SQL Query Performed by Activerecord in Ruby on Rails