Last active
August 29, 2015 14:00
-
-
Save mblackstock/11072996 to your computer and use it in GitHub Desktop.
OAuth2 with local wotkit instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requires the following packages installed. | |
# Flask, Flask-OAuthlib | |
# to test locally with out SSL, set environment variable DEBUG=true | |
from flask import Flask, request, url_for, session, jsonify, redirect, Response | |
import json | |
from flask_oauthlib.client import OAuth | |
app = Flask(__name__) | |
#app.debug = True #interferes with pydev source debugging | |
app.secret_key = 'development' | |
oauth = OAuth(app) | |
wotkit = oauth.remote_app('wotkit', | |
base_url='http://localhost:8080/api/', | |
request_token_url=None, | |
access_token_url='http://localhost:8080/api/oauth/token', | |
authorize_url='http://localhost:8080/api/oauth/authorize', | |
consumer_key='demo-app', | |
consumer_secret='df1bc6a3e89899e5' | |
) | |
@wotkit.tokengetter | |
def get_wotkit_token(token=None): | |
return session.get('wotkit_token') | |
@app.route('/oauth-authorized') | |
@wotkit.authorized_handler | |
def oauth_authorized(resp): | |
if resp is None: | |
return 'Access denied: reason=%s error=%s' % ( | |
request.args['error_reason'], | |
request.args['error_description'] | |
) | |
session['wotkit_token'] = (resp['access_token'], '') | |
users = wotkit.get('v2/users/me') | |
return Response(json.dumps(users.data), content_type='application/json') | |
@app.route('/login') | |
def login(): | |
return wotkit.authorize(callback=url_for('oauth_authorized', _external=True)) | |
@app.route("/") | |
def hello(): | |
if 'wotkit_token' in session: | |
users = wotkit.get('v2/users/me') | |
return Response(json.dumps(users.data), content_type='application/json') | |
return redirect(url_for('login')) | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment