首页 文章

Twython OAuth1使用示例代码发出401错误

提问于
浏览
3

我'm trying to setup a stream using the latest version of Twython with Python 2.7.3. I' m试图重现streaming docs中依赖于OAuth1 docs的例子 . 使用以下代码产生401错误,直到我终止执行:

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')

'OAUTH_TOKEN'和'OAUTH_TOKEN_SECRET'的值最终设置为unicode字符串 . 我将上面的'APP_KEY'和'APP_SECRET'设置为unicode字符串,两者都具有相同的结果 .

按照this reported issue中的建议,我更新了请求和请求-oauthlib,没有运气 .

我不相信我有防火墙问题 . 此时,我已经在不同的语言环境中的三个不同的盒子上尝试了这个代码,所有这些都具有相同的结果 .

不知道如何继续这一点 . 所有帮助赞赏 .

1 回答

  • 3

    运行类似下面的代码时会发生什么?

    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))
    

    我在github上打开了一个类似问题的问题,几乎在同一时间[1],因为我在这里遇到了同样的问题(我尝试了多个场景)


    UPDATE: 来自问题[1]中出现的解决方案 - 运行:

    ntpd -q
    

    那是因为系统时间有点偏差 . 好吧,对我来说工作 .

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

相关问题