Error with Ruby Twitter API

2019-02-06 12:47发布

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

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.

标签: ruby twitter
1条回答
男人必须洒脱
2楼-- · 2019-02-06 13:47

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.

For example:

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

For more information just refer to Sferik's Twitter Gem

查看更多
登录 后发表回答