Permalink
Browse files

Add client and fix bugs in app.py

  • Loading branch information...
lepture committed Nov 21, 2013
1 parent cbc3e12 commit 060da19663e006fab409b9e87639f8e00d3c8e22
Showing with 69 additions and 6 deletions.
  1. +12 −6 app.py
  2. +57 −0 client.py
View
18 app.py
@@ -198,17 +198,23 @@ def save_token(token, request, *args, **kwargs):
toks = Token.query.filter_by(
client_id=request.client.client_id,
user_id=request.user.id
).all()
)
# make sure that every client has only one token connected to a user
db.session.delete(toks)
for t in toks:
db.session.delete(t)
expires_in = token.pop('expires_in')
expires = datetime.utcnow() + timedelta(seconds=expires_in)
tok = Token(**token)
tok.expires = expires
tok.client_id = request.client.client_id
tok.user_id = request.user.id
tok = Token(
access_token=token['access_token'],
refresh_token=token['refresh_token'],
token_type=token['token_type'],
_scopes=token['scope'],
expires=expires,
client_id=request.client.client_id,
user_id=request.user.id,
)
db.session.add(tok)
db.session.commit()
return tok
View
@@ -0,0 +1,57 @@
from flask import Flask, url_for, session, request, jsonify
from flask_oauthlib.client import OAuth
CLIENT_ID = 'GbRmKgbSMmlE2NlugMeFfQIba8hoVyBFsWS8Igsq'
CLIENT_SECRET = 'BfP7jsN8dSsXjGLfTTPiEvarMJOpkZQ2Y7IVVee8X929LfolMV'
app = Flask(__name__)
app.debug = True
app.secret_key = 'secret'
oauth = OAuth(app)
remote = oauth.remote_app(
'remote',
consumer_key=CLIENT_ID,
consumer_secret=CLIENT_SECRET,
request_token_params={'scope': 'email'},
base_url='http://127.0.0.1:5000/api/',
request_token_url=None,
access_token_url='http://127.0.0.1:5000/oauth/token',
authorize_url='http://127.0.0.1:5000/oauth/authorize'
)
@app.route('/')
def index():
if 'remote_oauth' in session:
return session['remote_oauth'][0]
next_url = request.args.get('next') or request.referrer or None
return remote.authorize(
callback=url_for('authorized', next=next_url, _external=True)
)
@app.route('/authorized')
@remote.authorized_handler
def authorized(resp):
if resp is None:
return 'Access denied: reason=%s error=%s' % (
request.args['error_reason'],
request.args['error_description']
)
print resp
session['remote_oauth'] = (resp['access_token'], '')
return jsonify(oauth_token=resp['access_token'])
@remote.tokengetter
def get_oauth_token():
return session.get('remote_oauth')
if __name__ == '__main__':
import os
os.environ['DEBUG'] = 'true'
app.run(host='localhost', port=8000)

0 comments on commit 060da19

Please sign in to comment.