0

我正在尝试按照此处所述获取 Twitter 的 oauth request_token,并调用“oauth/request_token”:https ://dev.twitter.com/docs/auth/implementing-sign-twitter

我在这里使用 encode_params 函数生成参数:https ://github.com/sixohsix/twitter/blob/master/twitter/oauth.py

然后我用他们的键“授权”将返回的字符串包装在一个字典中,并将其转储到我正在使用 python 请求库发出的 Post 请求的 Headers 中。这是我用来创建请求的两行代码。

ep = "OAuth " + auth.encode_params(baseUrl, method, params)
response = requests.post(baseUrl+method, headers={ "Authorization" : ep})

最终的标题看起来像这样(修改了consumer_key):

{'Accept': '*/*',
 'Accept-Encoding': 'gzip, deflate, compress',
 'Authorization': 'OAuth oauth_callback=http%253A%252F%252Fec2-54-244-189-248.us-west-2.compute.amazonaws.com%252Fct%252Ftwitter_login_handler%252F&oauth_consumer_key=xxx&oauth_nonce=14937468581358710045&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1366568033&oauth_version=1.0&oauth_signature=kiYucZzPY%2FXy2WyJliJ6YcggVzQ%3D',
 'Content-Length': '0',
 'User-Agent': 'python-requests/1.2.0 CPython/2.7.3 Linux/3.5.0-21-generic'}

但是,我仍然收到 401 响应,上面写着:“无法验证 oauth 签名和令牌”

知道我做错了什么吗?任何帮助将不胜感激。

4

3 回答 3

0

我无法谈论您引用的脚本,但如果您愿意尝试另一个库作为rauth的作者,我可以推荐它。这是一个有效的 Twitter 示例。希望有帮助。

于 2013-04-27T13:30:46.703 回答
0

我最终使用了python-oauth2。他们的说明有点过时了,所以我更新了他们并提交了一个拉取请求。截至目前,它尚未被接受,但这里有一个指向带有更新说明的分叉回购的链接。

于 2013-04-28T20:04:06.740 回答
0

Hopefully this helps someone else... From the code referenced by maxcountryman - it has a comment that I had not found elsewhere till then :

# Get a real consumer key & secret from https://dev.twitter.com/apps/new

That helped me progress a lot... Cheers, Ian .

于 2013-08-14T16:45:30.263 回答