我尝试使用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/