-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfb.py
60 lines (48 loc) · 1.88 KB
/
fb.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import facepy
import yaml
from datetime import datetime, timedelta
from urlparse import parse_qs
def generate_extended_access_token(conf="config.yml"):
"""
Get an extended OAuth access token.
:param access_token: A string describing an OAuth access token.
:param application_id: An integer describing the Facebook application's ID.
:param application_secret_key: A string describing the Facebook application's secret key.
Returns a tuple with a string describing the extended access token and a datetime instance
describing when it expires.
"""
# Configuration
c = yaml.safe_load(open(conf))
app_id = c['fb_app_id']
app_secret = c['fb_app_secret']
temp_access_token = c['fb_temp_access_token']
# access tokens
default_access_token = facepy.get_application_access_token(
application_id=app_id,
application_secret_key=app_secret
)
graph = facepy.GraphAPI(default_access_token)
response = graph.get(
path='oauth/access_token',
client_id=app_id,
client_secret=app_secret,
grant_type='fb_exchange_token',
fb_exchange_token=temp_access_token
)
components = parse_qs(response)
token = components['access_token'][0]
expires_at = datetime.now() + timedelta(seconds=int(components['expires'][0]))
c['fb_stable_access_token'] = token
c['fb_stable_access_token_expires_at'] = expires_at.strftime("%Y-%m-%d %H:%M:%S")
with open(conf, 'wb') as f:
f.write(yaml.dump(c))
print "HERE IS YOUR STABLE ACCESS TOKEN: %s" % token
print "IT EXPIRES AT %s" % expires_at
print "YOUR CONFIG FILE (%s) HAS BEEN UPDATED" % conf
def connect(conf="config.yml"):
c = yaml.safe_load(open(conf))
return facepy.GraphAPI(c['fb_stable_access_token'])
if __name__ == '__main__':
generate_extended_access_token()