简体   繁体   中英

how can I capture response from twitter.com? ( ruby + twitter gem)

how can I capture response from twitter.com? To make sure that everything went ok?

I am using ruby and ruby twitter gem and the my code is basically like that

oauth = Twitter::OAuth.new('consumer token', 'consumer secret')
oauth.authorize_from_access('access token', 'access secret')

client = Twitter::Base.new(oauth)
client.update('Heeeyyyyoooo from Twitter Gem!')

The update twitter api method will send back a response that will let you know if everything went okay. It can respond in either json or xml, I'm sure the twitter gem is using one or the other as a default. You need to save the return value to a variable and parse it, if you have a status id in there then it worked. Try using a token or secret to check what happens when it errors. I would suggest changing your last line to this

ret = client.update('Heeeyyyyoooo from Twitter Gem!')

and then add this line below that to check out what you got back

puts ret.inspect

or

logger.info ret.inspect

or your choice of logging method

[Edit] It looked like the twitter gem hides the twitter api's actual response from you, parses it for you and just returns you the relevant bits. in the case of the update method it just returns you the id of your new tweet. you can view the id like this

puts ret.id

If you use another library to connect to the twitter api and need to parse xml or json responses then then the rest of this answer may be what you are looking for. [/Edit]

If you are not using a gem that parses twitter api responses for you then you will need to use something to parse the twitter api's responses into data that you can do something with. There are tons of ways to do this depending on what format you want to parse (json or xml)

My preferences:

Here is more information on what the twitter api update method returns: http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses%C2%A0update

This worked for me...

begin   
  resp = Twitter.update(params[:message])
rescue Exception => e
  # e.message contains the twitter response      
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM