3

I'm getting the following OAuth error when trying to make a request to the Twitter streaming api:

"#NoMethodError: undefined method `OAuth' for #TwitterMoment:0x007fa081d821f0"

def query
 authorisation_header = OAuth oauth_consumer_key=ENV["oauth_consumer_key"], oauth_nonce=ENV["oauth_nonce"], oauth_signature=ENV["oauth_signature"], oauth_signature_method=ENV["oauth_signature_method"], oauth_timestamp=ENV["oauth_timestamp"], oauth_token=ENV["oauth_token"], oauth_version=ENV["oauth_version"]
 response = HTTParty.get("https://stream.twitter.com/1.1/statuses/filter.json?locations=-#{@bounds}", headers: {"Authorization" => authorisation_header})
end

OAuth is included in my gemfile.

Any ideas would be very much appreciated! This is my first Stack Overflow question :)

amesimmons
  • 33
  • 2

1 Answers1

1

You're using OAuth here as a function/method, but that method doesn't exist. There's no def OAuth(...) anywhere in the oauth gem, so it explodes and gives you that NoMethodError.

Judging from the Header example at the bottom of this question, I think you've confused the header string for Ruby code.

Instead, you either need to make the string yourself (a bit annoying to do safely), or use the OAuth gem's methods (API) to do so.

Here's an example from the OAuth github repo:

consumer = OAuth::Consumer.new(
  options[:consumer_key],
  options[:consumer_secret],
  :site => "http://query.yahooapis.com"
)

access_token = OAuth::AccessToken.new(consumer)

response = access_token.request(
  :get,
  "/v1/yql?q=#{OAuth::Helper.escape(query)}&format=json"
)
rsp = JSON.parse(response.body)
pp rsp

This example may work for you (I'm not able to test it locally here, sorry):

def query
  consumer = OAuth::Consumer.new(
    ENV["oauth_consumer_key"],
    ENV["oauth_consumer_token"],
    site: "https://stream.twitter.com"
  )
  access_token = OAuth::AccessToken.new(consumer)

  response = access_token.request(
    :get,
    "/1.1/statuses/filter.json?locations=-#{OAuth::Helper.escape(@bounds)}"
  )
  response = JSON.parse(response.body)
  pp response  # Just a bit of debug printing for the moment; remove this later.
  response
end

An addendum:

Usually I might have directed you to use an existing Twitter client gem, such as https://github.com/sferik/twitter, but in this case it looks like they haven't implemented the Moments API yet.

Rob Howard
  • 1,789
  • 2
  • 20
  • 38
  • Once I read your addendum I realised I already had what I needed! I have this Twitter.rb file: $twitter = Twitter::Streaming::Client.new do |config| config.consumer_key = ENV["TWITTER_CONSUMER_KEY"] config.consumer_secret = ENV["TWITTER_CONSUMER_SECRET"] config.access_token = ENV["TWITTER_ACCESS_TOKEN"] config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"] end Query now this: `$twitter.filter(locations: @bounds) do |object| puts object.text if object.is_a?(Twitter::Tweet) end` and it seems to work! – amesimmons Jan 03 '16 at 06:02
  • The only problem is I'm now wondering if the streaming API might not be what I'm after. I'm trying to capture all tweets from a geographical location at a particular time. Tweets with images in particular. Originally I was using the search api, like this ... $twitter .search("e since:#{from_date} until:#{to_date}", geocode: "#{lat},#{long},10km", lang: "en", count: 100, filter: "images" ) ... but it wasn't returning many results! Even without the images filter it would only return 10 or 20 tweets. – amesimmons Jan 03 '16 at 06:06
  • I read that the search api isn't exhaustive. It only returns a sample of the most recent or popular tweets. And that the streaming api is better for this. – amesimmons Jan 03 '16 at 06:07
  • I'm out of my depth with the specifics of the Twitter API, sorry; I haven't done a Twitter integration since 2009. D: – Rob Howard Jan 03 '16 at 06:32
  • @amesimmons That's correct, search is based on relevance (internal Twitter API algorithm), rather than completeness: https://dev.twitter.com/rest/public/search – Joe Mayo Jan 03 '16 at 21:41