Skip to content

Instantly share code, notes, and snippets.

@cdiaz
Last active October 20, 2016 00:51
Show Gist options
  • Save cdiaz/5e5a792f004ba1d27f25e8d33d20449f to your computer and use it in GitHub Desktop.
Save cdiaz/5e5a792f004ba1d27f25e8d33d20449f to your computer and use it in GitHub Desktop.
from requests_oauthlib import OAuth2Session
from flask import Flask, request, redirect, session, url_for
from flask.json import jsonify
from urlparse import urlparse, parse_qs
import os
app = Flask(__name__)
client_id = "2727775315301"
client_secret = ""
authorization_base_url = 'http://chaira.udla.edu.co/api/oauth2/authorize.asmx/auth'
token_url = 'http://chaira.udla.edu.co/api/oauth2/authorize.asmx/token'
redirect_uri = 'http://localhost:8081/callback'
scope = ["read","write"]
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
@app.route("/login")
def login():
client = OAuth2Session(client_id, redirect_uri=redirect_uri)
authorization_url, state = client.authorization_url(authorization_base_url)
session['oauth_state'] = state
return redirect(authorization_url)
@app.route("/callback", methods=["GET"])
def callback():
response = parse_qs(urlparse(request.url).query)
if 'error' in response:
return jsonify({'error': response['error']})
client = OAuth2Session(client_id, state=session['oauth_state'], redirect_uri=redirect_uri)
token = client.fetch_token(token_url, client_secret=client_secret, authorization_response=request.url)
session['oauth_token'] = token
return redirect(url_for('.dashboard'))
@app.route("/dashboard", methods=["GET"])
def dashboard():
client = OAuth2Session(client_id, token=session['oauth_token'],redirect_uri=redirect_uri)
return jsonify(client.get("http://localhost:5000/me/").json())
if __name__ == "__main__":
app.secret_key = os.urandom(24)
app.run(port=3400,debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment