Twitter API via OmniAuth on Rails 3, Net :: HTTPUnauthorized hell

I followed the Simple OmniAuth tutorial ( http://asciicasts.com/episodes/241-simple-omniauth ) and I can log in with my Twitter account in the service. Now I want to access twitter API and twitter from the application. My code is as follows:

class TwitterController < ApplicationController

def prepare_access_token(oauth_token, oauth_token_secret)
consumer = OAuth::Consumer.new("KEY", "SECRET",
    {
        :site => "http://api.twitter.com"
    })
    # 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

def tweet
    # Exchange our oauth_token and oauth_token secret for the AccessToken instance.
    @access_token = prepare_access_token(current_user.token, current_user.secret)

    @response = @access_token.request(:post, "http://api.twitter.com/1/statuses/update.json", :status => "Tweet pela API")

    render :html => @response.body
end

end

The render string does nothing. Also, if I add

<p><%= @response %></p>

in my opinion i get

#<Net::HTTPUnauthorized:0x2ac5149e94f0>

However, I can get the username from my Twitter account.

My user model is as follows:

class User < ActiveRecord::Base
def self.create_with_omniauth(auth)  
       create! do |user|  
         user.provider = auth["provider"]  
         user.uid = auth["uid"]  
         user.name = auth["user_info"]["name"] 
         user.token = auth['credentials']['token'],
         user.secret = auth['credentials']['secret'] 
       end  
    end  
 end

What am I doing wrong?

+5
source share
2 answers

, . , .

class User < ActiveRecord::Base
    def self.create_with_omniauth(auth)  
       create! do |user|  
         user.provider = auth["provider"]  
         user.uid = auth["uid"]  
         user.name = auth["user_info"]["name"] 
         user.token = auth['credentials']['token']
         user.secret = auth['credentials']['secret'] 
       end  
    end  
end

.

+3

, , gem (gem install twitter.) . :

httpauth = Twitter::HTTPAuth.new(twitterName, twitterPass)
client = Twitter::Base.new(httpauth)
client.update(yourTweetText)
+2

All Articles