Skip to content

Instantly share code, notes, and snippets.

@justinabrahms
Created February 25, 2016 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinabrahms/0396bd4a0fe73b520ca1 to your computer and use it in GitHub Desktop.
Save justinabrahms/0396bd4a0fe73b520ca1 to your computer and use it in GitHub Desktop.
"""
Fetches some valid oauth credentials from edX, using only the secret & app key.
Based primarly off of the Facebook example in flask-oauth
"""
from flask import Flask, redirect, url_for, session, request
from flask_oauth import OAuth
SECRET_KEY = 'development key'
DEBUG = True
app = Flask(__name__)
app.debug = DEBUG
app.secret_key = SECRET_KEY
oauth = OAuth()
edx = oauth.remote_app(
'edx',
base_url='https://localhost.daplie.com:8000/',
request_token_url=None,
access_token_url='/oauth2/access_token/',
authorize_url='/oauth2/authorize/',
consumer_key='test-id',
consumer_secret='test-secret',
request_token_params={
'scope': 'email',
},
access_token_params={
'grant_type': 'authorization_code',
},
access_token_method='POST',
)
@app.route('/')
def index():
return redirect(url_for('login'))
@app.route('/login')
def login():
return edx.authorize(callback=url_for('edx_authorized',
next=request.args.get('next') or request.referrer or None,
_external=True))
@app.route('/login/authorized')
@edx.authorized_handler
def edx_authorized(resp):
print "RESP: %s" % resp
if resp is None:
return 'Access denied: reason=%s error=%s' % (
request.args['error_reason'],
request.args['error_description']
)
access = resp['access_token']
refresh = resp['refresh_token']
session['oauth_token'] = (access, '')
return "I have the access code:%s and refresh token:%s" % (access, refresh)
@edx.tokengetter
def get_edx_oauth_token():
return session.get('oauth_token')
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment