Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with Ruby Twitter API

Tags:

ruby

twitter

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.

like image 551
user1893354 Avatar asked Nov 27 '13 16:11

user1893354


People also ask

Is twitter using Ruby?

Twitter was at one time thought to be the largest Ruby on Rails shop in the world, and has made a substantial investment in its Ruby stack, going as far as developing its own generational garbage collector for Ruby called Kiji, which, unlike the standard Ruby collector, divides objects into generations and, on most ...

Can I use Twitter API without authentication?

You can do application-only authentication using your apps consumer API keys, or by using a App only Access Token (Bearer Token). This means that the only requests you can make to a Twitter API must not require an authenticated user.

How do I hit twitter API in Postman?

In Authorization tab, select OAuth 1.0. Enter your consumer key, consumer secret, access token and access token secret. Enable “Add params to header” and “Auto add parameters” Send the request.


Video Answer


1 Answers

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

like image 148
Nobita Avatar answered Sep 21 '22 15:09

Nobita