Twython OAuth1 issues, 401 error using example code

爱⌒轻易说出口 提交于 2019-12-07 13:23:33

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/17438943/twython-oauth1-issues-401-error-using-example-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!