-
Notifications
You must be signed in to change notification settings - Fork 0
/
exampleapp.py
97 lines (67 loc) · 2.54 KB
/
exampleapp.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
# -*- coding: utf-8 -*-
import base64
import os
import os.path
import urllib
import hmac
import json
import hashlib
from base64 import urlsafe_b64decode, urlsafe_b64encode
import requests
from flask import Flask, request, redirect, render_template, url_for
FB_APP_ID = os.environ.get('FACEBOOK_APP_ID')
requests = requests.session()
app_url = 'https://graph.facebook.com/{0}'.format(FB_APP_ID)
FB_APP_NAME = json.loads(requests.get(app_url).content).get('name')
FB_APP_SECRET = os.environ.get('FACEBOOK_SECRET')
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_object('conf.Config')
def get_home():
return 'https://' + request.host + '/'
def get_token():
if request.args.get('code', None):
return fbapi_auth(request.args.get('code'))[0]
cookie_key = 'fbsr_{0}'.format(FB_APP_ID)
if cookie_key in request.cookies:
c = request.cookies.get(cookie_key)
encoded_data = c.split('.', 2)
sig = encoded_data[0]
data = json.loads(urlsafe_b64decode(str(encoded_data[1]) +
(64-len(encoded_data[1])%64)*"="))
if not data['algorithm'].upper() == 'HMAC-SHA256':
raise ValueError('unknown algorithm {0}'.format(data['algorithm']))
h = hmac.new(FB_APP_SECRET, digestmod=hashlib.sha256)
h.update(encoded_data[1])
expected_sig = urlsafe_b64encode(h.digest()).replace('=', '')
if sig != expected_sig:
raise ValueError('bad signature')
code = data['code']
params = {
'client_id': FB_APP_ID,
'client_secret': FB_APP_SECRET,
'redirect_uri': '',
'code': data['code']
}
from urlparse import parse_qs
r = requests.get('https://graph.facebook.com/oauth/access_token', params=params)
token = parse_qs(r.content).get('access_token')
return token
@app.route('/', methods=['GET', 'POST'])
def index():
return render_template('index2.html')
@app.route('/channel.html', methods=['GET', 'POST'])
def get_channel():
return render_template('channel.html')
@app.route('/about.html', methods=['GET', 'POST'])
def get_channel():
return render_template('about.html')
@app.route('/close/', methods=['GET', 'POST'])
def close():
return render_template('close.html')
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
if app.config.get('FB_APP_ID') and app.config.get('FB_APP_SECRET'):
app.run(host='0.0.0.0', port=port)
else:
print 'Cannot start application without Facebook App Id and Secret set'