Authentication with Google App Engine from Iphone Native Client

I want to create an Iphone game in which users can log in with their facebook credentials and authenticate with my server running Google App Engine. I connected Facebook, working on both the iPhone and the Google App Engine. However, it looks like Facebook is connecting for the iPhone, only authenticates you to the Facebook server and allows you to access Facebook Api.

The reason I want to do this is because I can store additional user data associated with the user account in GAE, but also have them logged in with my Facebook credentials.

Is it possible to authenticate a user using their Facebook credentials using their own iPhone client, but authenticate on my server? Farmville for the iPhone seems to be doing something like this.

Any ideas how this is done?

+2
source share
2 answers

This is possible using the intermediate proxy server Facebook API of your GAE application.

By removing the code that I have, I call the classic API auth.getSessionwith auth_tokenand generate_session_secretprovided by the client, and of course format- XML.

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

, , , , , Facebook , Connect.

+1

- GAE, iphone, Facebook.

facebookProxy - URL GAE.

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()
0

All Articles