Streaming API Error Code 401 when REST API works fine?


#1

Didn’t know where else to go for this.

I’m using the twitter gem (https://github.com/sferik/twitter) in a ruby application to do two things: send a tweet and also stream based on 5-10 search terms.

I’m creating different client objects, a REST client for posting an update, and a Stream client for following the hashtags.

Here is the REST Client:

@twitter = Twitter::REST::Client.new do |config|
config.consumer_key = Settings.twitter.c_key
config.consumer_secret = Settings.twitter.c_sec
config.access_token = Settings.twitter.a_tok
config.access_token_secret = Settings.twitter.a_sec
end

Here is the Stream client:

@client = Twitter::Streaming::Client.new do |config|
config.consumer_key = Settings.twitter.c_key
config.consumer_secret = Settings.twitter.c_sec
config.access_token = Settings.twitter.a_tok
config.access_token_secret = Settings.twitter.a_sec
end

Note that the Settings class parameters are the same on both.

This works just fine:

@twitter.update(“foo”)

However this yields an error:

@client.filter(track: [“foo”,“bar”,“john”,“doe”].join(",")) do |object|
case object
when Twitter::Tweet
puts "@#{object.user.screen_name}: #{object.text}"
end
end

{:status_code=>401, :header=>{"WWW-Authenticate"=>"OAuth realm=\"Firehose\"", "Content-Type"=>"text/html", "Cache-Control"=>"must-revalidate,no-cache,no-store", "Transfer-Encoding"=>"chunked"}}

This seemed to work for around 24-48 hours after starting it. My terms list is not the example array above - the frequency of my terms is less than 100 in a 24 hour period. I don’t know where else to turn. Any help?


#2

Solved my issue! Better documentation on the Ruby gem website would be really helpful. I’ll try to edit and submit a PR.

Anyway here’s the fix:

You CANNOT put USERS into the track field. I want to filter on both terms and users - the way you’d do that is this:

@terms = [ "foo", "bar" ] @users_ids = [ "1234", "5678" ] @client.filter({track: @terms.join(",") , follow: @user_ids.join(",") }) do |object| case object when Twitter::Tweet puts "@#{object.user.screen_name}: #{object.text}" end end

NOTE: I converted the user_name to a user.id STRING value in the @user_ids array.

Not getting the authentication error anymore!

Hope this helps!