这是一个GAE代理,您可以使用Facebook连接从iPhone应用程序调用。
调用它的facebookProxy或任何和GAE上添加一个URL处理程序。
调用它使用的iPhone应用程序中:
session = [FBSession sessionForApplication:myApiKey getSessionProxy:@"http://yourApp.appspot.com/facebookProxy" delegate:self];
下面是代理的Python代码。我使用单独的常量文件来存储Facebook应用程序密钥,因此您需要更改以使用它。
import cgi
import hashlib
import httplib
import urllib
import logging
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import webapp
from google.appengine.api import users
import Constants
FB_API_HOST="api.facebook.com"
FB_API_PATH="/restserver.php"
def facebook_signature(paramsDict):
"""Compute the signature of a Facebook request"""
sorted = paramsDict.items()
sorted.sort()
trace = ''.join(["%s=%s" % x for x in sorted])
trace += Constants.FB_API_SECRET()
md5 = hashlib.md5()
md5.update(trace)
return md5.hexdigest()
def get_fb_session(auth_token):
"""Request a Facebook session using the given auth_token"""
params={
"api_key":Constants.FB_API_KEY,
"v":"1.0",
"auth_token":auth_token,
"generate_session_secret":"1",
"method":"auth.getSession",
}
params["sig"] = facebook_signature(params)
encoded_params = urllib.urlencode(params)
headers = {
"Content-type":"application/x-www-form-urlencoded",
}
conn = httplib.HTTPConnection(FB_API_HOST)
conn.request("POST", FB_API_PATH, encoded_params, headers)
logging.debug("%s" % encoded_params)
return conn.getresponse()
class FacebookSessionProxy(webapp.RequestHandler):
def get(self):
response = self.response
auth_token = self.request.get('auth_token')
logging.debug("AUTH TOKEN: %s" % auth_token)
if len(auth_token) == 0:
response.set_status(httplib.BAD_REQUEST)
response.out.write("Facebook login error: no auth_token given.")
return
fbResponse = get_fb_session(auth_token)
response.out.write(fbResponse.read())
response.set_status(fbResponse.status, fbResponse.reason)
response.headers['Content-Type'] = fbResponse.getheader('content-type')
# The End
application = webapp.WSGIApplication(
[
('/facebookproxy', FacebookSessionProxy),
],
debug=True)
def main():
logging.getLogger().setLevel(logging.DEBUG)
run_wsgi_app(application)
if __name__ == "__main__":
main()
那么一旦用户在服务器上有一个会话,你如何验证所有未来对服务器的请求呢? – 2013-02-22 17:53:18