python - Twitter oauth 与flask_oauthlib,无法生成请求 token

标签 python twitter oauth flask flask-oauthlib

我尝试使用flask_oauthlib 访问我的twitter api,但我得到的只是错误:无法生成请求 token 。这是代码。

    from flask_oauthlib.client import OAuth
    from flask import Flask, url_for, request, jsonify

    app = Flask(__name__)
    oauth = OAuth()

    twitter = oauth.remote_app(
        'twitter',
        base_url='https://api.twitter.com/1/',
        request_token_url='https://api.twitter.com/oauth/request_token',
        access_token_url='https://api.twitter.com/oauth/access_token',
        authorize_url='https://api.twitter.com/oauth/authorize',
        consumer_key='dOJjyxB6gxXWTjdtfPUZcZPjl',
        consumer_secret='im not telling you',
    )


    @app.route('/login')
    def login():
        return twitter.authorize(callback=url_for('authorized',
                                                  next=request.args.get('next') or request.referrer or None))


    @app.route('/authorized')
    @twitter.authorized_handler
    def authorized(resp):
        if resp is None:
            return 'Access denied: error=%s' % (
                request.args['error']
            )
        if 'oauth_token' in resp:
            # session['example_oauth'] = resp
            print(resp)
            return jsonify(resp)
        return str(resp)


    if __name__ == '__main__':
        app.run(port=8000, debug=True)

使用http://term.ie/oauth/example/client.php时这不起作用,我成功获得了请求 token 。

我用 https://github.com/lepture/example-oauth1-server/blob/master/client.py 激励自己和 http://flask-oauthlib.readthedocs.io/en/latest/client.html

编辑

奇怪的事实:我在这里尝试了代码:https://github.com/lepture/flask-oauthlib/blob/master/example/twitter.py 我没有更改 key 和 secret ,它有效。

因此,我尝试将它们更改为我自己的凭据,但它停止工作。实在是看不懂……

最佳答案

好的,我发现了问题。使用flask-oauthlib 时,回调 URL 似乎是必需的。所以我添加了一个假的,因为我仍在本地主机上,它解决了这个问题。

关于python - Twitter oauth 与flask_oauthlib,无法生成请求 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37950999/

相关文章:

python - Python 类是否像其他语言一样支持事件?

Python MySQL编码错误: 'latin-1' codec can't encode character: ordinal not in range(256)

java - Twitter4j 按位置+黑名单词查询

php - 通过 HTTP URL 查询 Gmail API

php - OAuth 与 O365 集成失败并出现错误 AADSTS65005

oauth - 通用分析和 Facebook 注册

python重写方法,无需重新复制整个代码

python - 无法在 Paraview 中导入 matplotlib

java - twitter4j 程序上出现 "unexpected token: int"错误

python - 如何从 datetime.datetime 对象中提取小时和分钟?