-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfacebook.py
executable file
·70 lines (58 loc) · 2.62 KB
/
facebook.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
from flask import Flask, redirect, url_for, session, request
from flask_oauth import OAuth
from lxml import etree
import optparse
SECRET_KEY = 'development key'
DEBUG = True
FACEBOOK_APP_ID = '161907000675044'
FACEBOOK_APP_SECRET = '9e41cc201fe5d20a5ed60ea4db592a2d'
oauth_token = ''
app = Flask(__name__)
app.debug = DEBUG
app.secret_key = SECRET_KEY
oauth = OAuth()
xmlFile = "settings.xml"
facebook = oauth.remote_app('facebook',
base_url='https://graph.facebook.com/',
request_token_url=None,
access_token_url='/oauth/access_token',
authorize_url='https://www.facebook.com/dialog/oauth',
consumer_key=FACEBOOK_APP_ID,
consumer_secret=FACEBOOK_APP_SECRET,
request_token_params={'scope': 'email'}
)
@app.route('/')
def index():
return redirect(url_for('login'))
@app.route('/login')
def login():
return facebook.authorize(callback=url_for('facebook_authorized',
next=request.args.get('next') or request.referrer or None,
_external=True))
@app.route('/login/authorized')
@facebook.authorized_handler
def facebook_authorized(resp):
if resp is None:
return 'Access denied: reason=%s error=%s' % (
request.args['error_reason'],
request.args['error_description']
)
session['oauth_token'] = (resp['access_token'], '')
me = facebook.get('/me')
tree = etree.parse(xmlFile)
for elem in tree.xpath(".//fb"):
elem.attrib['token'] = str(session.get('oauth_token')[0])
with open(xmlFile, 'w') as file_handle:
file_handle.write(etree.tostring(tree, pretty_print=True, encoding='utf8'))
return 'Successfully received Token! Logged in as id=%s name=%s redirect=%s. Please restart Crawler with Crawl options!' % \
(me.data['id'], me.data['name'], request.args.get('next'))
@facebook.tokengetter
def get_facebook_oauth_token():
return session.get('oauth_token')
if __name__ == '__main__':
print "Facebook Cr4wl3r Token Getter"
parser = optparse.OptionParser('usage fbCrawler.py -x <xml file> -c -m')
parser.add_option('-x', '--xml', dest='xmlFile', default="settings.xml", type='string', help='XML File containing settings')
(options, args) = parser.parse_args()
xmlFile = options.xmlFile
app.run()