Facebook接入(新)--iframe方式

    Facebook的文档跟进真是坑爹。接口换了,文档也好好换一换呐。好吧,不说它了, 老的方式接入,我写了一篇。现在再与时俱进一下,写一篇新的facebook接入。对应截止到2011年7月25日的Facebook API。

    如何设置开发者账号就不再啰嗦。用开发者账号创建了app之后,拿到App ID/API Key , App Secret ,记好Canvas Page。好,准备开始接入!

授权(authorization)

     为了给用户创造人性化(personalize)的体验,Facebook将把用户的信息,在用户允许的情况下发送给你的应用。这些信息将通过HTTP POST里的 signed_request 参数传给你的应用。 signed_request 其实就是一个经过base64url 编码的JSON对象。所以,在你解码之前,signed_request的内容看上去就是一个用点(.)分割的长串数据。

     用户第一次访问你的应用的时候,signed_request参数只包含下面这些数据(如果你看不到数据,只看到一长串字母数字,那请先看下边的解码签名请求)。可以看到,几乎没有什么有用的用户数据:

NameDescription
userA JSON array containing the locale string, country string and the age object (containing the min and max numbers of the age range) for the current user.  里面只有local,county,和age 没有uid 和token!
algorithmA JSON string containing the mechanism used to sign the request.
issued_atA JSON number containing the Unix timestamp when the request was signed.

     为了获取有用的用户信息,比如用户在Facebook的ID,那么你需要得到用户的授权。官方建议是使用认证对话框(OAuth Dialog) 进行用户对应用的授权。怎么引用这个对话框,进行授权呢?其实就是在你的服务器端或者页面端,跳转到 Facebook指定的URL。这个URL规则如下:

https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_CANVAS_PAGE

   当跳转到这个URL的时候,就会出现facebook标准的认证对话框了。注意,上面这种规则的URL授权对话框,你的应用只能是获取到用户的基本信息的,也就是facebook上用户的公共信息。例如ID,主页,用户的名字。如果你想获取到用户的其他信息,就必须明确的告诉用户,你的应用需要获取哪些用户的信息,像下面这样:

https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_stream

注意,scope参数里面,指定了你的应用想要获取的用户信息。

     假如用户点击了同意(allow),那么用户就对应用授权了。授权后,signed_request参数将含下面这些数据。可以看到,可以得到重要的两项:用户的id,就是user_id;和 oauth_token 。

NameDescription
userA JSON array containing the locale string, country string and the age object (containing the min and max numbers of the age range) for the current user.
algorithmA JSON string containing the mechanism used to sign the request.
issued_atA JSON number containing the Unix timestamp when the request was signed.
user_idA JSON string containing the Facebook user identifier (UID) of the current user.
oauth_tokenA JSON string that you can pass to the Graph API or the Legacy REST API.
expiresA JSON number containing the Unix timestamp when the oauth_token expires.


解码签名请求(Decode Signed Request)

     Facebook把请求做了签名。形成了signed_request这个东西。signed_request 其实就是一个经过base64url 编码的JSON对象。直接取过来,就是一个用"."分割的字符串。我们要做的,是把点前面的字符串解码,就是验证是否是facebook的合法sig;把点后面的字符串解码,就是facebook传给你应用的具体数据data。

   具体算法python版:

    # reques就是facebook发过来的请求
    params = request.POST.copy()        
    signed_request = params.get('signed_request')
    # signed_request传到python这边, 数据结构是一个字符串型的list
    if isinstance(signed_request, list):
        signed_request = signed_request[0]
    encoded_sig, payload = signed_request.split(".", 2)

    # 余数2, 那么需要补一个=
    payload = str(payload)
    if len(payload)%3 == 2:
        payload += '='
    # 余数1, 那么需要补两个=  
    if len(payload)%3 == 1:
        payload += '=='    
    # urlsafe_b64decode() Decode string s using a URL-safe alphabet, 
    # which substitutes - instead of + and _ instead of / in the standard Base64 alphabet.
    # 得到data    
    data = simplejson.loads(base64.urlsafe_b64decode(payload))
    
    # 得到sig
    encoded_sig = str(encoded_sig)
    if len(encoded_sig)%3 == 2:
        encoded_sig += '='
    if len(encoded_sig)%3 == 1:
        encoded_sig += '==' 
    sig = base64.urlsafe_b64decode(encoded_sig)

获取用户信息

     经过上面的解码签名请求后,可以获取到FB的数据了。就是上面代码里的data。如果里面有'user_id' 那么就说明,uid和 oauth_toke都有了。 请注意,如果您用的是国内服务器,是不可以直接调用facebook API接口的。

    if 'user_id' in data:
        uid = data['user_id']
        params_dic = {}
        params_dic['uid'] = uid
        params_dic['oauth_token'] = data['oauth_token']
  

   有了这uid和oauth_toke,我们就可以获取其他的用户信息了。下面是获取几个重要接入属性的方法。如果不明白为什么这么写,可以参考facebook开发文档。

   用户昵称

        graph_url = "https://graph.facebook.com/%s" % uid  
        facebook_usr_info = simplejson.loads( urllib2.urlopen(graph_url).read() )  
        name = facebook_usr_info['name']
    头像:

        headurl = 'http://graph.facebook.com/' + uid + '/picture' 
   好友列表:

        graph_url = "https://graph.facebook.com/me/friends?access_token=%s" % oauth_token
        f_dic = simplejson.loads(urllib2.urlopen(graph_url).read()) 
        friends_ids = f_dic['data'] 


   




评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值