Search code examples
rubytwittertwitter-oauth

fetching tweets with Ruby returns `open_http': 400 Bad Request (OpenURI::HTTPError)


I am new to Ruby. I have starting reading first chapter in Bastards Book Ruby i.e. Tweet fetching but the tutorial uses Twitter API v1. The response I got told me to use v1.1. So, I followed tutorials and wrote a program:

require 'oauth'

def prepare_access_token(oauth_token, oauth_token_secret)
    consumer = OAuth::Consumer.new("apiKey", "APISecret", { :site => "https://api.twitter.com", :scheme => :header })

    token_hash = { :oauth_token => oauth_token, :oauth_token_secret => oauth_token_secret }
    access_token = OAuth::AccessToken.from_hash(consumer, token_hash )

    return access_token
end

access_token = prepare_access_token("AccessToken", "AccessTokenSecret")

response = access_token.request(:get, "https://api.twitter.com/1.1/statuses/home_timeline.json")
puts response 

Output: #<Net::HTTPOK:0x007fb7eb0885b8>

If I run program until here. It runs just fine. But, then if I add below content to fetch tweets and write to file. I get errors

twitter_user = "iAbhimanyuAryan"
remote_base_url = "https://api.twitter.com/1.1/statuses/user_timeline.json?count=100&screen_name="

remote_full_url = remote_base_url + twitter_user

tweets = open(remote_full_url).read

my_local_filename = twitter_user + "-tweets.json"

my_local_file = open(my_local_filename, "w")
      my_local_file.write(tweets)

my_local_file.close    

Errors:

❯ ruby twitter.rb
#<Net::HTTPOK:0x007fbbfc9d75c8>
/Users/abhimanyuaryan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/open-uri.rb:358:in `open_http': 400 Bad Request (OpenURI::HTTPError)
    from /Users/abhimanyuaryan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/open-uri.rb:736:in `buffer_open'
    from /Users/abhimanyuaryan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/open-uri.rb:211:in `block in open_loop'
    from /Users/abhimanyuaryan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/open-uri.rb:209:in `catch'
    from /Users/abhimanyuaryan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/open-uri.rb:209:in `open_loop'
    from /Users/abhimanyuaryan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/open-uri.rb:150:in `open_uri'
    from /Users/abhimanyuaryan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/open-uri.rb:716:in `open'
    from /Users/abhimanyuaryan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/open-uri.rb:34:in `open'
    from twitter.rb:27:in `<main>'

Solution

  • Looking at the man pages for the Twitter API, this request requires authentication.

    You will need to use your AccessToken object to make the request. Something like this:

    # response is a [Net::HTTPResponse][3]
    response = access_token.get(remote_full_url)
    
    # Get the entire body of the response
    tweets = response.body
    

    Note, this is a very basic way of doing it. If you were putting such code into production, you would add proper checking and error handling.