-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
101 lines (82 loc) · 2.68 KB
/
app.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
from flask import Flask, redirect, url_for, render_template, flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, logout_user,\
current_user
from oauth import OAuthSignIn
# import facebook as fb
import urllib2
import json
from pprint import pprint
from flask_bootstrap import Bootstrap
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = 'top secret!'
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
app.config['OAUTH_CREDENTIALS'] = {
'facebook': {
'id': '201049237061460',
'secret': 'b0628ba953c46beba8b8dc9473d7d4c0'
},
'twitter': {
'id': '3RzWQclolxWZIMq5LJqzRZPTl',
'secret': 'm9TEd58DSEtRrZHpz2EjrV9AhsBRxKMo8m3kuIZj3zLwzwIimt'
}
}
db = SQLAlchemy(app)
lm = LoginManager(app)
lm.login_view = 'index'
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
social_id = db.Column(db.String(64), nullable=False, unique=True)
nickname = db.Column(db.String(64), nullable=False)
email = db.Column(db.String(64), nullable=True)
@lm.user_loader
def load_user(id):
return User.query.get(int(id))
@app.route('/')
def index():
return render_template('index.html')
@app.route('/listings')
def listings():
print "listings"
print access
url = "https://graph.facebook.com/v2.8/me/accounts?access_token={0}".format(access)
print url
a = urllib2.urlopen(url).read()
a = json.loads(a)
print a
return render_template('listings.html', a=a)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))
@app.route('/authorize/<provider>')
def oauth_authorize(provider):
if not current_user.is_anonymous:
return redirect(url_for('index'))
oauth = OAuthSignIn.get_provider(provider)
return oauth.authorize()
@app.route('/callback/<provider>')
def oauth_callback(provider):
if not current_user.is_anonymous:
return redirect(url_for('index'))
oauth = OAuthSignIn.get_provider(provider)
pprint (vars(oauth))
social_id, username, email, access_token = oauth.callback()
global access
access = access_token
print access
if social_id is None:
flash('Authentication failed.')
return redirect(url_for('index'))
user = User.query.filter_by(social_id=social_id).first()
if not user:
user = User(social_id=social_id, nickname=username, email=email)
db.session.add(user)
db.session.commit()
login_user(user, True)
return redirect(url_for('index'))
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port, debug=True)