-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient2.py
57 lines (44 loc) · 1.47 KB
/
client2.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
from flask import Flask, url_for, session, request, jsonify
from flask_oauthlib.client import OAuth
CLIENT_ID = 'dbp4NSwJXiYvbJKYoGQH9MXVotx8klMaDp9n0KQL'
CLIENT_SECRET = 'KnNrgTqG5DJtfDpaE4jzJ2gstfqGy5kTKG0tY9wpLC4Hp3mvLv'
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)