| import config |
| |
| __author__ = 'hanl' |
| |
| from flask import Flask, redirect, url_for, session, request, jsonify |
| from flask_oauthlib.client import OAuth |
| |
| app = Flask(__name__) |
| app.debug = True |
| app.secret_key = 'development' |
| oauth = OAuth(app) |
| korap = oauth.remote_app( |
| 'korap', |
| consumer_key=config.OAUTH2_CLIENT_ID, |
| consumer_secret=config.OAUTH2_CLIENT_SECRET, |
| request_token_params={}, |
| base_url='https://localhost:8443/api/v0.1', |
| request_token_url=None, |
| access_token_method='POST', |
| access_token_url='https://localhost:8443/api/v0.1/oauth2/token', |
| # should be a website of this flask application |
| authorize_url='https://localhost:8443/api/v0.1/oauth2/authorize', |
| ) |
| |
| |
| @app.route('/') |
| def index(): |
| if 'dropbox_token' in session: |
| me = korap.get('user/info') |
| return jsonify(me.data) |
| return redirect(url_for('login')) |
| |
| |
| @app.route('/login') |
| def login(): |
| return korap.authorize(callback=url_for('authorized', _external=True)) |
| |
| |
| @app.route('/logout') |
| def logout(): |
| session.pop('korap_token', None) |
| return redirect(url_for('index')) |
| |
| |
| @app.route('/login/authorized') |
| def authorized(): |
| resp = korap.authorized_response() |
| if resp is None: |
| return 'Access denied: reason=%s error=%s' % ( |
| request.args['error'], |
| request.args['error_description'] |
| ) |
| session['korap_token'] = (resp['access_token'], '') |
| me = korap.get('user/info') |
| return jsonify(me.data) |
| |
| |
| @korap.tokengetter |
| def get_korap_oauth_token(): |
| return session.get('korap_token') |
| |
| |
| |
| if __name__ == '__main__': |
| app.run() |