-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfacebookInterface.py
executable file
·115 lines (88 loc) · 3.71 KB
/
facebookInterface.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#
# Copyright (c) 2013 Bhautik J Joshi ([email protected])
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import findLibs
import authPickler
from apiToolsBase import OAuthObjectBase
from apiToolsBase import redirectToUrlText
import urllib2
import urlparse
import json
import web
from web import form
import facebook
class FacebookObject(OAuthObjectBase):
def __init__(self):
OAuthObjectBase.__init__(self)
self.apiName = "facebook"
def authPart1(self):
auth_url = 'https://www.facebook.com/dialog/oauth?client_id=' + self.api_key + \
'&redirect_uri=' + web.ctx.homedomain + '/auth/facebookAuth&scope=offline_access,publish_stream'
return auth_url
def authPart2(self, code):
access_token_url = 'https://graph.facebook.com/oauth/access_token?client_id=' + self.api_key + \
'&redirect_uri=' + web.ctx.homedomain + '/auth/facebookAuth' + \
'&client_secret=' + self.api_secret + '&code=' + code
pageGet = urllib2.urlopen(access_token_url).read()
self.final_oauth_token = urlparse.parse_qs(pageGet)['access_token'][0]
authDict = authPickler.getAuthDict(self.apiName)
authDict["final_oauth_token"] = self.final_oauth_token
authPickler.setAuthDict(self.apiName, authDict)
self.authorised = True
self.setupAPI()
return redirectToUrlText(web.ctx.homedomain)
def setupAPI(self):
self.facebookGraph = facebook.GraphAPI(self.final_oauth_token)
def postImagePost(self, jobDict):
paramsDict = {}
if jobDict["title"] != None:
paramsDict["name"] = jobDict["title"]
if jobDict["description"] != None:
paramsDict["description"] = jobDict["description"]
paramsDict["link"] = jobDict["flickrurl"]
paramsDict["picture"] =jobDict["flickrImageThumbUrl"]
self.facebookGraph.put_wall_post("",paramsDict)
def postTestPost(self):
self.facebookGraph.put_wall_post("TEST")
#-------------------------------------------------------------------------------
urls = (
"", "FacebookAuth",
)
render = web.template.render('templates/')
facebookObject = FacebookObject()
facebookObject.setup()
class FacebookAuth:
def GET(self, path=None):
global facebookObject
if not facebookObject.authorised:
params = web.input()
if 'code' in params.keys():
return facebookObject.authPart2(params['code'])
def POST(self):
params = web.input()
global facebookObject
if "submitInitial" in params.keys():
apiKey = params["apiKey"]
apiSecret = params["apiSecret"]
facebookObject.setupApp(apiKey, apiSecret)
redirectURL = facebookObject.authPart1()
return json.dumps({'redirectURL':redirectURL})
facebookApp = web.application(urls, locals())