Search code examples
rubyruby-on-rails-3twitter

how to GET and POST on Twitter in ruby on rails using access tokens


i am using omniauth to authenticate a user via twitter. omniauth provides access tokens. now i want to send the get or post request to twitter. i dont want to use any gems. i want to do with net::http.

even in twitter api documentation ! I am not able to find a good tutorial for this

can any one help? thanks


Solution

  • Here it is exactly what you need, so, since you've got the token and the secret from omniauth, now you are going to use it:

    def prepare_access_token(oauth_token, oauth_token_secret)
      consumer = OAuth::Consumer.new("APIKey", "APISecret", { :site => "https://api.twitter.com", :request_token_path => '/oauth/request_token', :access_token_path => '/oauth/access_token', :authorize_path => '/oauth/authorize', :scheme => :header })
      token_hash = { :oauth_token => oauth_token, :oauth_token_secret => oauth_token_secret }  
      access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
      access_token
    end
    

    Then you, for example, post a tweet:

    msg = {'status' => 'Hey look I can tweet via OAuth!'}
    access_token = prepare_access_token(token, secret)
    response = access_token.post('https://api.twitter.com/1/statuses/update.json', msg, { 'Accept' => 'application/xml' })
    

    Read the article presented on the link for more informations.