How to use Twitter gem in Rails, need small to figure out the whole thing?

懵懂的女人 提交于 2019-12-23 04:38:26

问题


I got access for the user using twitter_auth gem. Here is the code for that.

def twitter
  client = TwitterOAuth::Client.new(
  :consumer_key => '******',
  :consumer_secret => '********'
 )
 request_token = client.request_token(:oauth_callback => new_user_url)
 session[:request_token] = request_token
 redirect_to request_token.authorize_url
end


def new
   client = TwitterOAuth::Client.new(
      :consumer_key => '*****',
      :consumer_secret => '******'
      )
      access_token = client.authorize(
      session[:request_token].token,
      session[:request_token].secret,
      :oauth_verifier => params[:oauth_verifier]
      )
   #For testing purpose, i tried posting a status and its working perfectly fine

 client.update('I am authorized')
end

I am confused in using twitter gem cause every example from the docs says:

     Twitter.user("sferik").location // throws an error, Twitter::Error::Unauthorized: Invalid / expired Token

From friends and followers

    Twitter.accept("sferik") // throws an error, Twitter::Error::Unauthorized: Invalid / expired Token
    Twitter.follow("sferik") // throws an error, Twitter::Error::Unauthorized: Invalid / expired Token

All these errors makes sense, cause we are applying these methods on Class not an object. But how to create an object for this. I have a authorized user but how to take actions on his profile using token we got.


回答1:


Use client instead of Twitter.

You can see here how you should do it.



来源:https://stackoverflow.com/questions/8632547/how-to-use-twitter-gem-in-rails-need-small-to-figure-out-the-whole-thing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!