fb ios sdk session用于现有的fb connect网站

时间:2011-05-06 21:15:16

标签: iphone python objective-c facebook

我的fb连接网络服务器如何验证通过fb ios sdk登录的用户?我有一个使用facebook连接的网站。在其中,我使用app secret通过facebook javascript sdk通过facebook python库创建的cookie来验证用户:

def get_user_from_cookie(cookies, app_id, app_secret):
    """Parses the cookie set by the official Facebook JavaScript SDK.

    cookies should be a dictionary-like object mapping cookie names to
    cookie values.

    If the user is logged in via Facebook, we return a dictionary with the
    keys "uid" and "access_token". The former is the user's Facebook ID,
    and the latter can be used to make authenticated requests to the Graph API.
    If the user is not logged in, we return None.

    Download the official Facebook JavaScript SDK at
    http://github.com/facebook/connect-js/. Read more about Facebook
    authentication at http://developers.facebook.com/docs/authentication/.
    """
    cookie = cookies.get("fbs_" + app_id, "")
    if not cookie: return None
    args = dict((k, v[-1]) for k, v in cgi.parse_qs(cookie.strip('"')).items())
    payload = "".join(k + "=" + args[k] for k in sorted(args.keys())
        if k != "sig")
    sig = hashlib.md5(payload + app_secret).hexdigest()
    expires = int(args["expires"])
    if sig == args.get("sig") and (expires == 0 or time.time() < expires):
        return args
    else:
        return None

现在,我想知道如何将通过iphone登录的用户连接到我的网站。我是否只是将访问令牌发送到我的网络服务器,并根据访问令牌调用图表(只是绕过上述功能)?如果就是这样,那么上述功能提供的所有验证呢?

2 个答案:

答案 0 :(得分:0)

如果您想使用Facebook iOS API,则需要:

Alloc和Init一个Facebook对象

Facebook *facebookObject = [[Facebook alloc] init];

检查权限

NSArray *permissions = [NSArray arrayWithObjects:@"publish_stream", @"offline_access",nil];

授权facebook

[facebookObject authorize:APP_ID permissions:permissions delegate:self];

之后你可以使用NSUserDefaults保持这样的会话:

要存储会话,我使用NSUserDefaults。

[[NSUserDefaults standardUserDefaults] setObject:facebookObject.accessToken forKey:@"AccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:facebookObject.expirationDate forKey:@"ExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];

之后我可以通过以下方式访问Access:

facebookObject.accessToken    = [[NSUserDefaults standardUserDefaults] stringForKey:@"AccessToken"];

facebookObject.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:@"ExpirationDate"];

就像你可以保持会话活着,用户只输入一次登录名和密码。我认为这就是饼干的原因。

答案 1 :(得分:0)

Cookie获取和验证对Google AppEngine或JavaScript SDK访问非常有用。

要访问在iOS应用中启动的用户会话,请在Python SDK中使用:

graph = facebook.GraphAPI(access_token)
user = graph.get_object("me")

希望有所帮助。