简体   繁体   English

如何从twitter.com捕获响应? (红宝石+ Twitter宝石)

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

how can I capture response from twitter.com? 如何从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 我正在使用ruby和ruby twitter gem ,我的代码基本上是这样的

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. update twitter api方法将发送回响应,让您知道一切正常。 It can respond in either json or xml, I'm sure the twitter gem is using one or the other as a default. 它可以使用json或xml进行响应,我确定twitter gem使用默认的一个。 You need to save the return value to a variable and parse it, if you have a status id in there then it worked. 您需要将返回值保存到变量中并进行解析,如果那里有状态ID,则它可以工作。 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. [编辑]看起来twitter宝石隐藏了您对twitter api的实际响应,为您解析了它,只是返回了相关位。 in the case of the update method it just returns you the id of your new tweet. 对于update方法,它仅返回您新推文的ID。 you can view the id like this 您可以这样查看ID

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. 如果您使用另一个库连接到twitter api,并且需要解析xml或json响应,那么此答案的其余部分可能就是您想要的。 [/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. 如果您使用的不是为您解析Twitter api响应的gem,那么您将需要使用某些东西来将twitter api的响应解析为可以用来处理数据的数据。 There are tons of ways to do this depending on what format you want to parse (json or xml) 有多种方法可以执行此操作,具体取决于您要解析的格式(json或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 这是有关twitter api更新方法返回的信息的更多信息: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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