首页 文章

使用flask_oauthlib的Twitter oauth,无法生成请求令牌

提问于
浏览
2

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

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时无效,我设法获得了一个请求令牌 .

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

编辑

奇怪的事实:我在这里尝试了代码:https://github.com/lepture/flask-oauthlib/blob/master/example/twitter.py我没有改变密钥和秘密而且它有效 .

所以我尝试根据自己的凭据更改它们,它停止了工作 . 我真的无法理解......

2 回答

  • 1

    好的,我发现了问题 . 使用flask-oauthlib时,似乎必须使用回调URL . 所以我添加了一个假的,因为我还在localhost上,它解决了这个问题 .

  • 0

    如果有人发现这个问题 . 我是Flask-OAuthlib的作者 . 我建议你改用Authlib,浏览https://github.com/lepture/authlib的源代码 . https://github.com/authlib/loginpass中有许多内置的社交关系 .

相关问题