2

我能够让注册表单与自定义字段一起使用,并通过重定向页面上的 cookie 检索自定义字段。我想不通的是如何在下一页检索 Facebook 信息(姓名、电子邮件、生日)(我不是 PHP 人)。我已经多次阅读文档,“读取数据”部分(FB 文档),并在互联网上搜索,但只能找到 PHP 和 C# 解决方案。

如果有人可以帮助我,我正在寻找 javascript、jQuery 或 ColdFusion 解决方案。

下面是我的代码作为快速参考。就目前而言,第 2 页只是转储 cookie 信息。

第 1 页

<html>
<head>
  <title>My Facebook Login Page</title>
</head>
<body>

<!--- Facebook Call --->
  <div id="fb-root"></div>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : 'myAppID', // App ID
        channelUrl : 'http://.../channel.html', // Channel File
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
      });
    };
    // 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));
  </script>

<!--- Output Facebook Box---> 
<fb:registration redirect-uri="http://.../page2.cfm" 
    fields='[
        {"name":"name"},
        {"name":"email"},
        {"name":"displayName","description":"Display Name:", "type":"text"},
        {"name":"location"},
        {"name":"birthday"}
        ]'
    onvalidate="validateAndSave">
</fb:registration>

<!---Validation + set Cookie --->
<script>
     function validateAndSave(form) {
          if (!form.displayName) {
            return({"displayName":"Display Name is Required"});
          }
              document.cookie = 'fbInfo='+'displayName='+form.displayName;
              return {};
        }
</script>
</body>

4

1 回答 1

0

这是我基于 FB javascript API 反复试验的实现。

function checkFB()
{   
    if (typeof(FB) === "undefined")
    {
        var t = setTimeout("checkFB()", 1000);
        if (debug) console.log("FB is still undefined, try again in 1 second.");
    }
    else
    {
        if (debug) console.log("FB is defined, try to login -> ");

        //Function to check the current login status of facebook            
        FB.getLoginStatus(function(r)
        {
            if (r.status === "not_authorized")
            {
                if (debug) console.log("-> NOT AUTHORIZED ");
            }
            else if (r.status === "connected")
            {
                if (debug) console.log("-> connected ");

                if (r.authResponse) 
                {
                    FB.api('/me', function(rr) {
                        document.getElementById("fbFname").value = rr.first_name;
                        document.getElementById("fbLname").value = rr.last_name;
                        document.getElementById("fbEmail").value = rr.email;
                        document.getElementById("fbID").value = rr.id;
                        document.getElementById("status").value = "OK";
                    });

                    submitFB();
                } 
                else 
                {
                    document.getElementById("status").value = "NOT AUTHORIZED";
                    history.go(-1);
                    if (debug) console.log("NOT AUTHORIZED");
                }
            }
            else
            {
                if (debug) console.log("-> not connected -> POP UP BOX expected ");
                // start login process
                FB.login(function(re) 
                {
                    if (re.authResponse) 
                    {
                        FB.api('/me', function(rr) {
                            document.getElementById("fbFname").value = rr.first_name;
                            document.getElementById("fbLname").value = rr.last_name;
                            document.getElementById("fbEmail").value = rr.email;
                            document.getElementById("fbID").value = rr.id;
                            document.getElementById("status").value = "OK";
                        });
                        submitFB();
                    } 
                    else 
                    {
                        document.getElementById("status").value = "NOT AUTHORIZED";
                        history.go(-1);
                        if (debug) console.log("NOT AUTHORIZED");
                    }
                }, 
                {scope: 'email'}); // we define what information we want to get from the user
            }
        });
    }
}

我希望你已经从 facebook 获得了一个 appID 并且有一个频道文件,他们需要替换 'myAppID' 和 'http://.../channel.html'。

 FB.init({
        appId      : 'myAppID', // App ID
        channelUrl : 'http://.../channel.html', // Channel File
于 2012-10-24T08:40:17.793 回答