I tried to use flask_oauthlib to access my twitter api, but all I get is the error : Failed to generate request token. Here is the code.
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)
This didn’t work while using http://term.ie/oauth/example/client.php, I managed to get a request token.
I inspired myself with https://github.com/lepture/example-oauth1-server/blob/master/client.py and http://flask-oauthlib.readthedocs.io/en/latest/client.html
EDIT
Weird fact : I tried the code here : https://github.com/lepture/flask-oauthlib/blob/master/example/twitter.py
I didn’t changed the key and secret and it worked.
So I tried to change them for my own credentials, and it stopped working. I really can’t understand…
2
Answers
Ok I found the problem. It appears that the callback URL is mandatory when using flask-oauthlib. So I added a fake one since i'm still on localhost, and it solved this problem.
In case anyone found this issue. I’m the author of Flask-OAuthlib. I suggest that you use Authlib instead, browser the source code at https://github.com/lepture/authlib. There are many built-in social connections in https://github.com/authlib/loginpass.