-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPyFacebook.py
94 lines (64 loc) · 2.39 KB
/
PyFacebook.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
import urllib
import urllib2
import cgi
import json
import ConfigParser
import facebook
class PyFacebook:
def __init__(self, config_fname = '.config'):
self.conf_fname = config_fname
self.get_config()
if self.app_code == 'None':
self.authorize()
self.authenticate()
self.graph = facebook.GraphAPI(self.token)
def get_config(self):
config = ConfigParser.ConfigParser()
config.read(self.conf_fname)
self.app_id = config.get('AppInfo', 'app_id')
self.app_secret = config.get('AppInfo', 'app_secret')
self.app_code = config.get('AppInfo', 'code')
def authenticate(self):
url = 'https://graph.facebook.com/oauth/access_token'
redirect_uri = 'http://www.facebook.com/connect/login_success.html'
post_data = urllib.urlencode([
('redirect_uri', redirect_uri),
('client_id', self.app_id),
('client_secret', self.app_secret),
('code', self.app_code),
])
request = urllib2.Request(url, post_data)
response = urllib2.urlopen(request)
content = '&'.join(response.read().split())
query = cgi.parse_qs(content)
self.token = query['access_token'][0]
response.close()
def authorize(self):
print 'dss'
url = 'https://graph.facebook.com/oauth/authorize'
redirect_uri = 'http://www.facebook.com/connect/login_success.html'
post_data = urllib.urlencode([
('redirect_uri', redirect_uri),
('client_id', self.app_id),
('scope', 'offline_access,read_stream,user_location,friends_location'),
])
request = urllib2.Request(url, post_data)
response = urllib2.urlopen(request)
content = '&'.join(response.read().split())
query = cgi.parse_qs(content)
self.code = query['code'][0]
response.close()
#write it to config
config = ConfigParser.ConfigParser()
config.read(self.conf_fname)
config.set('AppInfo', 'code', self.code)
def main():
fb = PyFacebook()
#api_id, api_secret, code = Config('.config')
#token = authenticate(api_id, api_secret, code)
#graph = facebook.GraphAPI(token)
graph = fb.graph
print graph.get_object("me")['first_name']
print graph.get_object("754775853")
if __name__ == '__main__':
main()