/
gminifb.py
261 lines (198 loc) · 8.52 KB
/
gminifb.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"""gminifb, Minimal facebook api.
Stateless functions to access the facebook api.
Version 1.1
September 21, 2007
Peter Shinners
Hacked by Patrick Crosby, April, 2008, to try to get it working on Google's appengine
(http://blog.patrickcrosby.com/posts/35-Write-Facebook-apps-using-Google-AppEngine)
The documentation for the Facebook methods and arguments and return
values is well written. http://developers.facebook.com/documentation.php
This was developed on Python 2.4, but should easily run on other versions.
By default this requires the 'simplejson' module. But if requesting
raw text in XML or JSON modes then it is not used or imported.
http://cheeseshop.python.org/pypi/simplejson
This code is released under the MIT license.
# Examples of usage
import minifb
_FbApiKey = "xxxyyyzzz..."
_FbSecret = minifb.FacebookSecret("aaabbbccc...")
def ModPythonHandler(request):
'''FBML Canvas page receives session info from POST'''
arguments = minifb.validate(_FbSecret, request.read())
if arguments["added"] != "1":
return ServeIndexNonmember()
else:
session_key = arguments["session_key"]
uid = arguments["user"]
return ServeIndex(uid, session_key)
def UserAdded(request):
'''Facebook callback when user has added application
gets an auth_token through post that must be converted
into a session_key. Then lookup and send stuff to Facebook'''
# Parse and validate posted values
arguments = minifb.validate(_FbSecret, request.read())
auth_token = arguments["auth_token"]
# Request session_key from auth_token
result = minifb.call("facebook.auth.getSession",
_FbApiKey, _FbSecret, auth_token=auth_token)
uid = result["uid"]
session_key = result["session_key"]
# Lookup username and details
usersInfo = minifb.call("facebook.users.getInfo",
_FbApiKey, _FbSecret, session_key=session_key,
call_id=True, fields="name,pic_square",
uids=uid) # uids can be comma separated list
name = usersInfo[0]["name"]
photo = usersInfo[0]["pic_square"]
AddUserToDatabase(uid, name, photo)
# Set the users profile FBML
fbml = "<p>Welcome, new user, <b>%s</b></p>" % name
minifb.call("facebook.profile.setFBML",
_FbApiKey, _FbSecret, session_key=session_key,
call_id=True, uid=uid, markup=fbml)
"""
# Copyright (c) 2007 Peter Shinners
# 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.
__all__ = ["FacebookError", "call", "validate", "FacebookSecret"]
import md5
import time
import cgi
import urllib
from google.appengine.api import urlfetch
import logging
# Globals
_fbUrl = "http://api.facebook.com/restserver.php"
_fbApi = "1.0"
class FacebookError(StandardError):
"""Error that happens during a facebook call."""
def __init__(self, error_code, error_msg):
self.error_code = error_code
self.error_msg = error_msg
StandardError.__init__(self, "Facebook Error %s: %s"
% (self.error_code, self.error_msg))
def call(method, api_key, secret, **kwargs):
"""Call facebook server with a method request. Most keyword arguments
are passed directly to the server with a few exceptions.
The 'sig' value will always be computed automatically.
The 'v' version will be supplied automatically if needed.
The 'call_id' defaults to True, which will generate a valid
number. Otherwise it should be a valid number or False to disable.
The default return is a parsed simplejson object.
Unless the 'format' and/or 'callback' arguments are given,
in which case the raw text of the reply is returned. The string
will always be returned, even during errors.
If an error occurs, a FacebookError exception will be raised
with the proper code and message.
The secret argument can be any string, but should be an
instance of FacebookSecret to hide value from simple
introspection.
"""
# Finalize kwargs
call_id = kwargs.get("call_id", True)
if call_id is True:
kwargs["call_id"] = int(time.time() * 1000)
elif call_id is False:
del kwargs["call_id"]
customFormat = "format" in kwargs or "callback" in kwargs
kwargs.setdefault("format", "JSON")
kwargs.setdefault("v", _fbApi)
kwargs.setdefault("api_key", api_key)
kwargs.setdefault("method", method)
# Hash with secret key
md5hash = md5.new()
for key, value in sorted(kwargs.items()):
md5hash.update(key)
md5hash.update("=")
md5hash.update(str(value))
md5hash.update(FacebookSecret.value(secret))
del secret # No longer using secret, clear it from locals
kwargs["sig"] = md5hash.hexdigest()
logging.info("Calling facebook: method = " + method)
# Call website, send arguments as POST
args = urllib.urlencode(kwargs) # should pass doseq=True?
logging.info("args = " + args)
response = urlfetch.fetch(_fbUrl, payload=args, method=urlfetch.POST, headers = {'Content-type': 'application/x-www-form-urlencoded'})
# Handle response
if customFormat:
return response
import simplejson
data = simplejson.loads(response.content)
try:
raise FacebookError(data["error_code"], data["error_msg"])
except (LookupError, TypeError):
pass
return data
def validate(secret, request):
"""Validate the arguments received from facebook. This is usually
sent for the iframe in Facebook's canvas. It is not necessary
to use this on the auth_token and uid passed to callbacks like
post-add and post-remove.
request is a google webapp Request object
If the data is invalid or not signed properly, and empty
dictionary is returned.
The secret argument can be any string, but should be an
instance of FacebookSecret to hide value from simple
introspection.
"""
signature = request.get("fb_sig")
if not signature:
return []
argument_names = request.arguments()
# Hash data with secret key
prefix = "fb_sig_"
preflen = len(prefix)
unsigned = {}
signed = []
for i in argument_names:
if i.startswith(prefix):
signed.append((i[preflen:], request.get(i)))
else:
unsigned[i] = request.get(i)
signed.sort()
md5hash = md5.new()
for key, value in signed:
md5hash.update(key)
md5hash.update("=")
md5hash.update(value)
md5hash.update(FacebookSecret.value(secret))
del secret # No longer using secret, clear it from locals
if md5hash.hexdigest() != signature:
# Hash is incorrect, return unsigned fields
return unsigned
unsigned.update(signed)
return unsigned
class FacebookSecret(object):
"""Simple container that stores a secret value. Will prevent fancy
traceback tools like cgitb and django from showing the secret key
in error reports.
The static value method will convert this or any object to
a string.
"""
def __init__(self, value):
b = buffer(value)
del value
self.__call__ = lambda: str(b)
# Odd to mask this as a builtin. It doesn't fool vars(), but
# class instances are still not callable, TypeError.
def __str__(self):
return "<FacebookSecret>"
__repr__ = __str__
@staticmethod
def value(obj):
if isinstance(obj, FacebookSecret):
return obj.__call__()
return str(obj)