Facebook注册插件使用Javascript SDK从signed_request获取注册数据

时间:2013-04-19 17:26:22

标签: javascript facebook registration

我正在尝试使用javascript SDK使用facebook的注册插件。据我所知,以下是获取注册数据的步骤:

1)显示注册块(如果消费者登录到facebook,则预先填写信息) 2)消费者填写所有信息并点击注册。 3)我们从facebook获得加密的签名请求。 4)解码signed_request以获得“代码”。 5)验证此代码是否正确。 6)使用此代码,获取访问令牌 7)使用访问令牌,我们可以获得姓名,电子邮件,性别等消费者数据。

我可以让我的代码工作到第4步。

a)之后我想知道如何验证“代码” b)现在我跳过了第5步,当我尝试使用“代码”获取“访问令牌”时,我得到错误-191:重定向URI必须是绝对URI。

这是我的代码,请指导我解决这个问题:

<script>
          window.fbAsyncInit = function() {
        FB.init({
            appId      : 'xxx', // App ID
            channelUrl : 'http:// localhost.qualityhealth.com/channel.html', 
            status     : true, // check login status
            cookie     : true, // enable cookies to allow the server to access the session
            xfbml      : true,  // parse XFBML
            oauth      : true
        });

        // Additional initialization code such as adding Event Listeners goes here
        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {

                var signedRequest = response.authResponse.signedRequest;
                var verifier = parse_signed_request(signedRequest, 'CLIENT_SECRET');
                var code = verifier.code;

                FB.api('https://graph.facebook.com/oauth/access_token?   client_id=CLIENT_ID&client_secret=CLIENT_SECRET&redirect_uri=http://localhost.qualityhealth.com&code=' + code, function(response1) {
                    console.log(response1);

                });
            }
        });

    };
    // Load the SDK Asynchronously
    (function(d){
        var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
        if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
    }(document));


    function parse_signed_request(signed_request, secret) {

        encoded_data = signed_request.split('.',2);
        // decode the data
        sig = encoded_data[0];
        json = decode64(encoded_data[1]);
        data = JSON.parse(json); 

        // check algorithm - not relevant to error
        if (!data.algorithm || data.algorithm.toUpperCase() != 'HMAC-SHA256') {
            console.error('Unknown algorithm. Expected HMAC-SHA256');
            return null;
        }

        // check sig - not relevant to error

        console.log(data);
        return data;
    }

    function decode64(s) {
        var e={},i,b=0,c,x,l=0,a,r='',w=String.fromCharCode,L=s.length;
        var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        for(i=0;i<64;i++){e[A.charAt(i)]=i;}
        for(x=0;x<L;x++){
            c=e[s.charAt(x)];b=(b<<6)+c;l+=6;
            while(l>=8){((a=(b>>>(l-=8))&0xff)||(x<(L-2)))&&(r+=w(a));}
        }
        return r;
    };

</script>
<div id="fb-root"></div>

<!--- Output Facebook Box--->
<fb:registration redirect-uri="http://localhost.qualityhealth.com"
                 fields='[
        {"name":"name"},
        {"name":"first_name"},
        {"name":"email"},
        {"name":"state", "description":"State", "type":"text"},
        {"name":"gender"},
        {"name":"phone",      "description":"Phone Number", "type":"text"},
        {"name":"birthday"}
        ]'
        >
</fb:registration>

提前感谢您的帮助。

0 个答案:

没有答案