Repository navigation
Expand file tree
/
Copy pathgithub.py
More file actions
61 lines (46 loc) · 1.49 KB
/
Copy pathgithub.py
File metadata and controls
61 lines (46 loc) · 1.49 KB
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
61
from flask import Flask, redirect, url_for, session, request, jsonify
from flask_oauthlib.client import OAuth
app = Flask(__name__)
app.debug = True
app.secret_key = 'development'
oauth = OAuth(app)
github = oauth.remote_app(
'github',
consumer_key='a11a1bda412d928fb39a',
consumer_secret='92b7cf30bc42c49d589a10372c3f9ff3bb310037',
request_token_params={'scope': 'user:email'},
base_url='https://api.github.com/',
request_token_url=None,
access_token_method='POST',
access_token_url='https://github.com/login/oauth/access_token',
authorize_url='https://github.com/login/oauth/authorize'
)
@app.route('/')
def index():
if 'github_token' in session:
me = github.get('user')
return jsonify(me.data)
return redirect(url_for('login'))
@app.route('/login')
def login():
return github.authorize(callback=url_for('authorized', _external=True))
@app.route('/logout')
def logout():
session.pop('github_token', None)
return redirect(url_for('index'))
@app.route('/login/authorized')
@github.authorized_handler
def authorized(resp):
if resp is None:
return 'Access denied: reason=%s error=%s' % (
request.args['error_reason'],
request.args['error_description']
)
session['github_token'] = (resp['access_token'], '')
me = github.get('user')
return jsonify(me.data)
@github.tokengetter
def get_github_oauth_token():
return session.get('github_token')
if __name__ == '__main__':
app.run()