Ruby Twitter API 错误 [英] Error with Ruby Twitter API

查看:42
本文介绍了Ruby Twitter API 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 'twitter' gem 的 Ruby 脚本中遇到错误.我的脚本中产生错误的部分是

I am getting an error with a Ruby script using the 'twitter' gem. The part of my script that is producing the error is

require 'twitter'
require 'net/http'
require 'json'

#### Get your twitter keys & secrets:
#### https://dev.twitter.com/docs/auth/tokens-devtwittercom
Twitter.configure do |config|
  config.consumer_key = 'xxxxxxx'
  config.consumer_secret = 'xxxxxxx'
  config.oauth_token = 'xxxxxx'
  config.oauth_token_secret = 'xxxxxxx'
end

错误说未定义方法'configure' for Twitter:Module (NoMethodError)然而,'twitter' 和 'json' gems 都在我的 gemfile 中,所以我不确定为什么这个方法是未定义的.

The error says undefined method 'configure' for Twitter:Module (NoMethodError) However the 'twitter' and 'json' gems are both in my gemfile so I'm not sure why this method would be undefined.

推荐答案

您正在以旧"方式进行操作.从版本 5 开始,不再提供全局配置.所以,基本上你需要在初始化客户端时传递配置参数.

You are doing it the "old" way. Starting in Version 5, global configuration is not longer available. So, basically you need to pass the config parameters when you initialize a client.

例如:

client = Twitter::REST::Client.new do |config|
  config.consumer_key        = "YOUR_CONSUMER_KEY"
  config.consumer_secret     = "YOUR_CONSUMER_SECRET"
  config.access_token        = "YOUR_ACCESS_TOKEN"
  config.access_token_secret = "YOUR_ACCESS_SECRET"
end

然后就用那个客户端做查询,比如:

And then just use that client to do queries, such as:

client.sample do |tweet|
  puts tweet.text
end

有关更多信息,请参阅 Sferik 的 Twitter Gem

For more information just refer to Sferik's Twitter Gem

这篇关于Ruby Twitter API 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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