PHP Streaming API was working fine, now returning 401 Unauthorized error

oauth
streaming
php
phirehose

#1

Using a php web application using phirehose streaming API to collect and display tweets according to chosen keywords. It was working fine since months but now giving HTTP ERROR 401 when trying to connect with Oauth as follows -

Connecting to ssl://stream.twitter.com, port=443
Error 401 Unauthorized
Problem accessing '/1.1/statuses/filter.json’
Reason: Unauthorized

Nothing has changed in the app and I cannot find what is wrong.Please help. Thanks.


#2

Anyone, please help !!!


#3

Are the application consumer key and secret still valid?

Is the time on the server where your code is running accurate?


#4

Yes, Server time is set to exact local time.
Consumer key and secret are also valid. I even tried regenerating them recently.


#5

OK. You said you’d made no changes recently, but obviously you would have changed the key and secret in the code (and I assume if you are using static access tokens you also regenerated those when you changed the consumer key).

I’d try to isolate the issue using the simplest possible test case. Are you able to use twurl to connect to the streaming endpoint with the same filter and the same consumer key and secret, for example? Does the code work on any other machine?

Based on the 401 error there would usually also be a JSON response from the Twitter API indicating what else might be the cause of the issue. Can you capture that and output it to the console / log?


#6

Thanks for your help and response.

I changed consumer key and secret only after reading suggestions on this forum in similar threads. But the same did not work.

I am using phirehose to connect. You can find the code here - https://github.com/lancetharper/140dev

I have already posted the JSON response in my leading post. Getting Error 401 Unauthorized.


#7

That’s the HTTP response code, not the error response from the Twitter API.

I’m not able to run that code as it doesn’t have documentation describing how to do so. The version of Phirehose in that repository is also outdated and does not reflect the most recent updates.

Has this suddenly stopped working after being connected for a long period of time? When was the last time you connected successfully?


#8

How to get the error response from the Twitter API, please guide

Yes, I am using updated version of Phirehose as mentioned in the branch - https://github.com/twitteractivism/140dev/tree/master/libraries/phirehose

I was able to use this for two months and last time I connected was 2 days back.


#9

Probably a good idea to check with the author of the Phirehose library or the code you are using - I’m not an expert in that.


#10

Just have to run the get_tweets_keyword.php script. No response to emails from the author as of yet.


#11

That code seems to work fine here using my own app keys (although I had to modify it not to require a database connection) so I’m not sure what could be causing it to fail. If you’ve been trying to run multiple copies of this then there’s a chance your IP might have been restricted, but I don’t think that would return a 401 error.


#12

You are saying that from one IP, only one connection to twitter streaming site can be established ? I had read that from one app, one connection can be made.

How can I check if my IP is restricted or not ? And what should I do if it is restricted ?


#13

Confirmed. The IP has been blocked by twitter. It is working on another machine with another IP.

How to find out reason for blockage of IP and how to get the IP unblocked ?


#14

That’s most likely due to multiple connections or not respecting the back off period for reconnections. I’d raise a ticket via the platform support forms as we are unable to help you with this via these forums.


#15

Can you please tell whether it is allowed to run two separate streams from same IP and different apps ?

Say 2 sites share same IP and are running twitter streams from different apps and keys/tokens ? Is it permitted by twitter ?


#16

It depends on the use case, for example user streams would work with different user tokens, but the public sample and filter endpoints are not intended for multiple connections.

If you repeatedly try to connect to the streaming API too quickly, or with too many streams per IP address, then this throttling can occur. For simple, occasional one-off or single continuous stream connections then it should be fine.

The streaming API is not designed for large volume, multiple connections for many users on a single IP basis. The deprecated site streams API (which is being removed) was an initial attempt at that, but it is now being replaced with the account activity API which is webhook-based.

If you require larger volumes of data then the commercial Gnip APIs are available.


#17

Andy,

Say I need TWO (only two and not more) large volume stream connection to run , then I should not use both of them from the same IP, is that correct ?


#18

The support link (https://support.twitter.com/forms/platform) which you gave is only for Twitter App related issues.

But how can I get my IP restrictions by twitter removed ?


#19

That’s the correct form to use to ask about this - this is an app-related issue. We cannot help you via the forums.

You may be able to concurrently run two streams, but generally you’ll find that they may disconnect one another. You should not use multiple IP addresses or machines for the same use case in order to circumvent the Twitter API limits, as this is against the developer policy.


#20

I sent email to them but they did not respond. How is it possible to contact them ?