/
github.py
111 lines (87 loc) · 3.1 KB
/
github.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# github.py
from flask import (Flask, flash, request, redirect,
render_template, url_for, session)
from flask.ext.sqlalchemy import SQLAlchemy
from rauth.service import OAuth2Service
# Flask config
SQLALCHEMY_DATABASE_URI = 'sqlite:///github.db'
SECRET_KEY = '\xfb\x12\xdf\xa1@i\xd6>V\xc0\xbb\x8fp\x16#Z\x0b\x81\xeb\x16'
DEBUG = True
# Flask setup
app = Flask(__name__)
app.config.from_object(__name__)
db = SQLAlchemy(app)
# Use your own values in your real application
github = OAuth2Service(
name='github',
base_url='https://api.github.com/',
access_token_url='https://github.com/login/oauth/access_token',
authorize_url='https://github.com/login/oauth/authorize',
client_id = '09be269976304f03264a',
client_secret = '3f51d253a9f992d9030ae923602a6445798fb80d',
)
# models
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
login = db.Column(db.String(80), unique=True)
name = db.Column(db.String(120))
def __init__(self, login, name):
self.login = login
self.name = name
def __repr__(self):
return '<User %r>' % self.login
@staticmethod
def get_or_create(login, name):
user = User.query.filter_by(login=login).first()
if user is None:
user = User(login, name)
db.session.add(user)
db.session.commit()
return user
# views
@app.route('/')
def index():
return render_template('login.html')
@app.route('/about')
def about():
if session.has_key('token'):
auth = github.get_session(token = session['token'])
resp = auth.get('/user')
if resp.status_code == 200:
user = resp.json()
return render_template('about.html', user = user)
else:
return redirect(url_for('login'))
@app.route('/login')
def login():
#redirect_uri = url_for('authorized', next=request.args.get('next') or
# request.referrer or None, _external=True)
redirect_uri = url_for('authorized', _external=True)
print(redirect_uri)
# More scopes http://developer.github.com/v3/oauth/#scopes
params = {'redirect_uri': redirect_uri, 'scope': 'user:email'}
print(github.get_authorize_url(**params))
return redirect(github.get_authorize_url(**params))
# same path as on application settings page
@app.route('/github/callback')
def authorized():
# check to make sure the user authorized the request
if not 'code' in request.args:
flash('You did not authorize the request')
return redirect(url_for('index'))
# make a request for the access token credentials using code
redirect_uri = url_for('authorized', _external=True)
data = dict(code=request.args['code'],
redirect_uri=redirect_uri,
scope='user:email,public_repo')
auth = github.get_auth_session(data=data)
# the "me" response
me = auth.get('user').json()
user = User.get_or_create(me['login'], me['name'])
session['token'] = auth.access_token
session['user_id'] = user.id
flash('Logged in as ' + me['name'])
return redirect(url_for('index'))
if __name__ == '__main__':
db.create_all()
app.run()