Twython OAuth1问题,使用示例代码显示401错误 [英] Twython OAuth1 issues, 401 error using example code

查看:252
本文介绍了Twython OAuth1问题,使用示例代码显示401错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用最新版本的Twython和Python 2.7.3来设置流.我试图在流文档中重现该示例. OAuth1文档.使用以下代码会产生401错误,直到我终止执行:

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

'OAUTH_TOKEN'和'OAUTH_TOKEN_SECRET'的值最终设置为unicode字符串.我已经将'APP_KEY'和'APP_SECRET'设置为上述或设置为Unicode字符串,两者的结果相同.

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.

按照此报告的问题中的建议,我同时更新了请求和请求-oauthlib运气.

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.

推荐答案

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

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

我几乎同时[1]在github上提出了一个类似问题的问题,因为我在这里遇到了同样的问题(并且我已经尝试了不止一种情况)

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)

更新: 从问题[1]中提出的解决方案中进行-运行:

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

这篇关于Twython OAuth1问题,使用示例代码显示401错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆