|
1 from flask import Flask, url_for, session, request, jsonify |
|
2 from flask_oauthlib.client import OAuth |
|
3 from settings.client_settings import ClientSettings |
|
4 |
|
5 |
|
6 |
|
7 app = Flask(__name__) |
|
8 app.debug = True |
|
9 app.secret_key = 'secret' |
|
10 app.config.from_object(ClientSettings) |
|
11 oauth = OAuth(app) |
|
12 |
|
13 remote = oauth.remote_app( |
|
14 'remote', |
|
15 consumer_key=app.config.get("CLIENT_ID", ""), |
|
16 consumer_secret=app.config.get("CLIENT_SECRET", ""), |
|
17 request_token_params=app.config.get("REQUEST_TOKEN_PARAMS", ""), |
|
18 base_url=app.config.get("BASE_URL", ""), |
|
19 request_token_url=app.config.get("REQUEST_TOKEN_URL", ""), |
|
20 access_token_url=app.config.get("ACCESS_TOKEN_URL", ""), |
|
21 authorize_url=app.config.get("AUTHORIZE_URL", "") |
|
22 ) |
|
23 |
|
24 |
|
25 @app.route('/') |
|
26 def index(): |
|
27 if 'remote_oauth' in session: |
|
28 resp = remote.get('me') |
|
29 return jsonify(resp.data) |
|
30 next_url = request.args.get('next') or request.referrer or None |
|
31 return remote.authorize( |
|
32 callback=url_for('authorized', next=next_url, _external=True) |
|
33 ) |
|
34 |
|
35 |
|
36 @app.route('/authorized') |
|
37 def authorized(): |
|
38 resp = remote.authorized_response() |
|
39 if resp is None: |
|
40 return 'Access denied: reason=%s error=%s' % ( |
|
41 request.args['error_reason'], |
|
42 request.args['error_description'] |
|
43 ) |
|
44 print resp |
|
45 session['remote_oauth'] = (resp['access_token'], '') |
|
46 return jsonify(oauth_token=resp['access_token']) |
|
47 |
|
48 |
|
49 @remote.tokengetter |
|
50 def get_oauth_token(): |
|
51 return session.get('remote_oauth') |
|
52 |
|
53 |
|
54 if __name__ == '__main__': |
|
55 import os |
|
56 os.environ['DEBUG'] = 'true' |
|
57 os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'true' |
|
58 app.run(host='localhost', port=8000) |