从Iphone本机客户端通过Google App Engine进行身份验证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从Iphone本机客户端通过Google App Engine进行身份验证相关的知识,希望对你有一定的参考价值。

我想创建一个Iphone游戏,用户可以使用其Facebook凭据登录并通过我在Google App Engine上运行的服务器进行身份验证。 我已经在iPhone和Google App Engine上都使用了Facebook Connect。 但是,似乎用于iPhone的Facebook连接仅通过Facebook的服务器对您进行身份验证,并允许您从中访问Facebook Api。

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

是否可以使用iPhone本机客户端使用其Facebook凭据来认证用户,但可以通过我的服务器进行认证? 看来iPhone的Farmville就是这样做的。

任何想法如何做到这一点?

答案

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

略过我执行此操作的一些代码,我使用客户端提供的auth_tokengenerate_session_secret调用经典API的auth.getSession ,当然formatXML

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

本质上,发生的事情是客户端登录,获取身份验证令牌,将其发布到中间代理,然后中间代理要求Facebook使用该令牌进行会话,然后将其返回给Connect客户端。

另一答案

这是一个GAE代理,您可以使用Facebook connect从您的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()

以上是关于从Iphone本机客户端通过Google App Engine进行身份验证的主要内容,如果未能解决你的问题,请参考以下文章