forked from authlib/example-oauth2-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
60 lines (47 loc) · 1.67 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from flask import Flask, url_for, session, request, jsonify
from flask_oauthlib.client import OAuth
CLIENT_ID = '7duz2iEw0NfPKB4lQcf3tSMgT8q8kDX1fNQLuBFt'
CLIENT_SECRET = 'nFZdS8WGGyUTmmgsKgShtZ45myJfmha33wk0K2CRAjEgOsPuYH'
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://docker-dev-1.cirg.washington.edu:5000/api/',
request_token_url=None,
access_token_url='http://docker-dev-1.cirg.washington.edu:5000/oauth/token',
authorize_url='http://docker-dev-1.cirg.washington.edu:5000/oauth/authorize'
)
@app.route('/')
def index():
if 'remote_oauth' in session:
resp = remote.get('me')
return jsonify(resp.data)
#next_url = request.args.get('next') or request.referrer or None
next_url = request.args.get('next') or None
return remote.authorize(
callback=url_for('authorized', next=next_url, _external=True)
)
@app.route('/authorized')
def authorized():
resp = remote.authorized_response()
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'
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'true'
app.run(host='0.0.0.0', port=8000)