Upgrading Twitter Gem to API v1.1

Are you using old Ruby’ Twitter Gem below v.4* and implementing Search API to find something and now nothing listed in the search results? Well, it’s happened to me last night too. As we know, Twitter has announced regarding to deprecation of version 1 API and will gradually force all access to their API to use version 1.1. I know this issue and just ignored it because i didn’t take the annoucements seriously :D

But, what is different version 1.1 and the earlier versions for Search API? It’s simply that twitter don’t want to allow any unauthenticated access grabbing data from their platform. Because earlier version of Search API don’t need us to create application client through their developer platform and their data is just available by accessing their search endpoint. This case might be bad for Twitter because maybe so many requests come from a certain IP but they don’t understand who are accessing too many data.

So, let’s talk the technical stuff. You have to update your twitter gem version to version above 4.0:

gem 'twitter', '~> 4.0'

Add consumer key and secret to your Twitter initialization in your source code or you will get “Bad Authenticaiton data” errors:

Twitter.configure do |config|
  config.consumer_key = 'xxx'
  config.consumer_secret = 'aaa'
end

Add oauth token and oauth token secret you get from developer.twitter.com after creating an application and authenticate a user to your Twitter client in your source code or you will get another authentication errors:

@client   = Twitter::Client.new(oauth_token: 'yyy',oauth_token_secret: 'zzz')

And the last one, the Twitter Gem is now using method results from their search results object and you might have to adjust it:

@results = @client.search....
@results #Twitter::SearchResults array of objects

now we should use it like this

Twitter::SearchResults.results
@search.results

Hopefully this is helps..

blog comments powered by Disqus