2

我想创建一个Iphone游戏,用户可以使用他们的Facebook凭据登录并使用在Google App Engine上运行的服务器进行身份验证。我已经获得了Facebook连接iPhone和Google App Engine的工作。然而,Facebook的Facebook连接似乎只能通过Facebook的服务器进行身份验证,并允许您从Facebook访问Facebook Api。使用Google App Engine从Iphone Native Client进行身份验证

我想这样做的原因是,我可以在GAE上存储与用户帐户关联的额外用户数据,但也让他们用他们的Facebook凭证登录。

是否可以使用iPhone本机客户端使用其Facebook身份验证用户身份,但通过我的服务器进行身份验证?对于iPhone来说,Farmville似乎就是这样做的。

任何想法如何完成?

回答

1

这可以通过GAE应用程序上的中间Facebook API代理实现。

一些代码,略读我做这个,我称之为经典API auth.getSessionauth_token和客户端提供generate_session_secret,当然format的是XML

http://developers.facebook.com/docs/reference/rest/auth.getSession/

本质上发生的事情是在客户端登录,获取认证令牌,职位,以中间代理,然后要求Facebook上的会话中使用该令牌,即返回给客户端连接。

+0

那么一旦用户在服务器上有一个会话,你如何验证所有未来对服务器的请求呢? – 2013-02-22 17:53:18

0

这是一个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() 
相关问题