如何捕获来自 twitter.com 的响应?(红宝石 + 推特宝石) [英] how can I capture response from twitter.com? ( ruby + twitter gem)

查看:30
本文介绍了如何捕获来自 twitter.com 的响应?(红宝石 + 推特宝石)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何捕获来自 twitter.com 的响应?确保一切顺利?

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

我正在使用 ruby​​ 和 ruby twitter gem,我的代码基本上就是这样

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!')

推荐答案

更新 twitter api 方法将发回一个响应,让您知道一切是否正常.它可以以 json 或 xml 响应,我确定 twitter gem 使用其中一个作为默认值.您需要将返回值保存到一个变量并解析它,如果您在那里有一个状态 ID,那么它就可以工作了.尝试使用令牌或秘密来检查出错时会发生什么.我建议将您的最后一行更改为此

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

logger.info ret.inspect

或您选择的日志记录方法

or your choice of logging method

看起来 twitter gem 对你隐藏了 twitter api 的实际响应,为你解析它并只返回相关位.在 update 方法的情况下,它只返回您的新推文的 id.你可以这样查看id

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

如果您使用另一个库连接到 twitter api 并需要解析 xml 或 json 响应,那么这个答案的其余部分可能就是您正在寻找的.[/编辑]

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]

如果您没有使用为您解析 twitter api 响应的 gem,那么您将需要使用一些东西将 twitter api 的响应解析为您可以使用的数据.根据您要解析的格式(json 或 xml),有很多方法可以做到这一点

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)

我的偏好:

以下是有关 twitter api 更新方法返回内容的更多信息:http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses%C2%A0update

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

这篇关于如何捕获来自 twitter.com 的响应?(红宝石 + 推特宝石)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆