Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twython OAuth1 issues, 401 error using example code

I'm trying to setup a stream using the latest version of Twython with Python 2.7.3. I'm trying to reproduce the example in the streaming docs which depend on the OAuth1 docs. Using the following code yields 401 errors until I kill execution:

from twython import Twython
from twython import TwythonStreamer

class MyStreamer(TwythonStreamer):
    def on_success(self, data):
        if 'text' in data:
            print['text'].encode('utf-8')

    def on_error(self, status_code, data):
        print status_code


APP_KEY    = 'mupAeFE44nOU5IlCo4AO0g' # Consumer key in twitter app OAuth settings
APP_SECRET = 'NOTMYSECRET0zo1WbMAeSIzZgh1Hsj9CrlShonA'  # Consumer secret in OAuth settings

twitter = Twython(APP_KEY,APP_SECRET)
auth = twitter.get_authentication_tokens()

OAUTH_TOKEN        = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']

stream = MyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
stream.statuses.filter(track = 'twitter')

The values of 'OAUTH_TOKEN' and 'OAUTH_TOKEN_SECRET' end up set to unicode strings. I've set 'APP_KEY' and 'APP_SECRET' as above or as unicode strings both with the same results.

Following the advice in this reported issue I updated both requests and requests-oauthlib without luck.

I don't believe I'm having firewall issues. At this point, I've tried this code on three different boxes all in different locales all with the same results.

Not sure how to proceed at this point. All help appreciated.

like image 343
Sevenless Avatar asked Feb 05 '26 09:02

Sevenless


1 Answers

What happens when you run something like the code below?

from twython import Twython

CONSUMER_KEY = "****"
CONSUMER_SECRET = "****"

OAUTH_TOKEN = "****"
OAUTH_TOKEN_SECRET = "******"

twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

import json
print(json.dumps(twitter.get_user_timeline(screen_name='jonatascd')[0], indent=2))

I've openend an issue at github with a similar question, almost at the same time [1], because I'm with the same problem here (and I've tried in more than one scenario)


UPDATE: from the solution that came up in the issue [1] - run:

ntpd -q

that is because the system time was a little off. Well, for me worked.

[1] https://github.com/ryanmcgrath/twython/issues/237

like image 163
Jonatas CD Avatar answered Feb 06 '26 22:02

Jonatas CD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!