Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twitter Stream API + OAuth not working #133

Closed
kuzeko opened this issue Mar 25, 2013 · 18 comments
Closed

Twitter Stream API + OAuth not working #133

kuzeko opened this issue Mar 25, 2013 · 18 comments

Comments

@kuzeko
Copy link

kuzeko commented Mar 25, 2013

I've used the code some months ago, with the streaming API and the OAuth method and it worked.

Today I tried again and I'm getting a

HTTP Error 401: Unauthorized

passing through

 stream.py", line 52, in handle_stream_response

and originating in

 urllib2.py, line 521, in http_error_default

The same OAuth works fine with other methods: I tried both
statuses.home_timeline()
and
direct_messages.new()

@pykler
Copy link
Contributor

pykler commented Mar 25, 2013

try doing a

twitter_client.account.verify_credentials()

And see if you oauth keys are still available.

@pykler pykler closed this as completed Mar 25, 2013
@pykler pykler reopened this Mar 25, 2013
@kuzeko
Copy link
Author

kuzeko commented Mar 25, 2013

the verify method gave

HTTP Error 401: Unauthorized

again

I successfully read the home timeline and sent a direct message, isn't that a sign of the validity of the OAuth keys?

@pykler
Copy link
Contributor

pykler commented Mar 25, 2013

are you using the verify method on api.twitter.com or stream.twitter.com, you should change the domain to api.twitter.com when testing the verify method.

@pykler
Copy link
Contributor

pykler commented Mar 25, 2013

Yes that is true, if you have sent tweets then your oauth is working. That error is comming straight from twitter though so it seems like a temporary issue on their end.

@kuzeko
Copy link
Author

kuzeko commented Mar 25, 2013

Point is that if I use the normal UserPassAuth it works...

@sixohsix
Copy link
Collaborator

Hi,

Did you recently upgrade to PTT 1.9.2? The default API version for the streaming class changed to '1.1'. Is it possible that the command you are running is no longer available in the 1.1 API? You can pass version='1' to the TwitterStream constructor to get back the old behaviour (so long as Twitter continues to support that version).

In API 1.1 I don't think there is a home timeline, in fact. The correct call is now:

TwitterStream(domain='userstream.twitter.com', auth=OAuth(...)).user()

See: https://dev.twitter.com/docs/api/1.1/get/user

@kuzeko
Copy link
Author

kuzeko commented Mar 26, 2013

Hi,
yes I've upgraded to the latest version.
I'm trying this one

https://dev.twitter.com/docs/api/1.1/get/statuses/sample

This is the code that gives the error

twitter_stream = TwitterStream(domain='stream.twitter.com', auth=OAuth( oauth_token, oauth_secret,CONSUMER_KEY,  CONSUMER_SECRET))
iterator = twitter_stream.statuses.sample()

@sixohsix
Copy link
Collaborator

Okay, thanks for the info. I just tested this exact code and it worked for me. I was able to iterate tweets from the sample stream with PTT 1.9.2, Python 2.7.

Are the consumer token and secrets the same ones you authorized with originally? Even if so, maybe try doing the oauth_dance again to get new oauth creds.

If you are still having trouble, please let me know what OS and Python version you are using.

@kuzeko
Copy link
Author

kuzeko commented Mar 26, 2013

This is strange...
oauth_dance renewed, I can send DMs and do other calls like the normal (non streaming) user timeline, but not accessing the stream via OAuth.
On the other end, I confirm that if I switch to the plain UserPassAuth everything works, even the stream.

I also tried the curl equivalent command, and it worked.
I'm running python 2.7 on GNU/Linux Mint

@sixohsix
Copy link
Collaborator

Wait wait... note the difference in the domain. I'm using userstream.twitter.com, not stream.twitter.com. Userstream is the new 1.1 server to use. (Other streams have other servers now... confusingly.)

@kuzeko
Copy link
Author

kuzeko commented Mar 26, 2013

I'm not sure to understand... or I may have made some confusion

I tried the following just now

twitter = TwitterStream(domain='userstream.twitter.com', auth=oauth)
iterator = iterator = twitter.statuses.sample()

and I have the error.
Then I run

twitter = TwitterStream(domain='userstream.twitter.com', auth=UserPassAuth(user, pwd))
iterator = iterator = twitter.statuses.sample()

and it works.

changing to domain='stream.twitter.com' makes no difference :

  • first doesn't work
  • second still works

@pykler
Copy link
Contributor

pykler commented Mar 26, 2013

It seems your there is something wrong with your oauth credentials. Oauth
is working fine for me.

Using: twitter 1.9.2

@kuzeko
Copy link
Author

kuzeko commented Mar 26, 2013

It seems that my OAuth credentials are working ( @pykler I mentioned it in my original post up here ):

  • I used them through curl and they worked
  • I used them in the following code, and they worked:

twitter = Twitter(auth=oauth)
iterator = twitter.statuses.home_timeline()
twitter.direct_messages.new(user="Kuzeko", text="Check-check-check")

@kuzeko
Copy link
Author

kuzeko commented Mar 28, 2013

Is there a way to have print out the actual request the code is making?
Because, since the curl call is working, I would like to compare it to the call that the python is executing.

Thanks

@kuzeko
Copy link
Author

kuzeko commented Apr 8, 2013

It seems I'm dwelling in the merciless pit of cannot reproduce ... I'll try to deal with it, can you please answer to this:

Is there a way to have print out the actual request the code is making?

Thank you!

@placebofx
Copy link

Thanks! I was having the same issue but now I have a healthy stream again!

Is there a wiki for this code? I would love to contribute.

Thanks again

@kuzeko
Copy link
Author

kuzeko commented Jun 21, 2013

What and how please?

@kuzeko
Copy link
Author

kuzeko commented Jul 17, 2013

Here it is guys!
If you encounter problems with OAuth and a wild

HTTP Error 401: Unauthorized

appears, the answer (in 90% of the cases) is:

sudo  ntpdate -b pool.ntp.org

Yes, your system has lost a minute too much

@kuzeko kuzeko closed this as completed Jul 17, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants