Forum: Ruby Need help using Oauth gem for twitter

0faae51834740c2142efb5af4b8d8175?d=identicon&s=25 Rochit Sen (rochitsen)
on 2014-07-22 04:33
Hi All,
I have been trying to use the oauth gem with twitter. Followed the below
but i am getting "invalid or expired token". The code below is from
twitter site itself. Maybe i am passing wrong arguments?

require 'oauth'
require 'yaml'

# Exchange your oauth_token and oauth_token_secret for an AccessToken
instance.
def prepare_access_token(oauth_token, oauth_token_secret)
  consumer = OAuth::Consumer.new("API_key", "API_Secret",
    { :site => "https://api.twitter.com/1.1",
      :scheme => :header
    })
  # now create the access token object from passed values
  token_hash = { :oauth_token => oauth_token,
                 :oauth_token_secret => oauth_token_secret
               }
  access_token = OAuth::AccessToken.from_hash(consumer, token_hash)
  return access_token
end

# Exchange our oauth_token and oauth_token secret for the AccessToken
instance.
access_token = prepare_access_token("consumer_key", "consumer_secret")
# use the access token as an agent to get the home timeline
response = access_token.request(:get,
"https://api.twitter.com/1.1/statuses/home_timeline...)
puts response.to_yaml


I have the following already provided by twitter which i am using in the
above code.
-Consumer_key
-Consumer_secret
-API_Key
-API_secret

I am printing the last line (response) to yaml to see what if i have
been authorized or not.
Help much appreciated.
Thanks
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.