0

嗨,我是 facebook APP 的新手。我正在研究 Facebook C# SDK 和 Javascript SDK。但问题是我无法获得 AccessToken,也无法调试代码。请找到以下代码

<div id="fb-root"></div> <!-- This initializes the FB controls-->   
<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: 'XXXXXXXXXXXXXXX',   // App ID
            status: true,               // check login status
            cookie: true,               // enable cookies to allow the server to access the session
            xfbml: true                 // parse XFBML             
        });

        // Additional initialization code here
        alert("Additional initialization code here");
        FB.Event.subscribe('auth.login', function (response) 
        {
            debugger; ****//Debugger is not Raising****
            if (response.status === 'connected') {
                // connected
                alert("Connected");
            } else if (response.status === 'not_authorized') {
                // not_authorized
            } else {
                // not_logged_in
            }
        });

        FB.getLoginStatus(function (response) {
            if (response.session) {
                // logged in and connected user, someone you know
                alert("Connected");
            } else {
                // no user session available, someone you dont know
            }
        });

        FB.Event.subscribe('auth.authResponseChange', function (response) {
            alert("Additional initialization code here");
            if (response.status === 'connected') {
                alert("Bhuvan");  `// Event Not Raising`
                // the user is logged in and has authenticated your
                // app, and response.authResponse supplies
                // the user's ID, a valid access token, a signed
                // request, and the time the access token 
                // and signed request each expire
                debugger;
                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;
                // TODO: Handle the access token

                // Do a post to the server to finish the logon
                // This is a form post since we don't want to use AJAX
                var form = document.createElement("form");
                form.setAttribute("method", 'post');
                form.setAttribute("action", 'FacebookLogin.aspx');

                var field = document.createElement("input");
                field.setAttribute("type", "hidden");
                field.setAttribute("name", 'accessToken');
                field.setAttribute("value", accessToken);
                form.appendChild(field);

                document.body.appendChild(form);
                form.submit();

            } else if (response.status === 'not_authorized') {
                // the user is logged in to Facebook, 
                // but has not authenticated your app
            } else {
                // the user isn't logged in to Facebook.
            }
        });

    };

        // 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> 
    <div class="fb-login-button"  data-show-faces="true" data-width="400" data-max-rows="1"></div>

我能够登录,但我无法处理任何事件。我可以知道为什么我无法引发事件,以及为什么我无法检索访问代码吗?

4

1 回答 1

1

首先通过在脚本上方添加此代码块来异步加载 SDK,即在此行上方window.fbAsyncInit = function () {

添加这个:

// 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));

还在 FB.init({}); 中添加通道文件

channelUrl: '//' + window.location.hostname + '/Beta/channel.html', // Path to your Channel File LIVE

在根文件夹 文件内容中创建一个名为 channel.html 的 html 文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script src="//connect.facebook.net/en_US/all.js"></script>
</head>
<body>

</body>
</html>
于 2012-11-16T09:09:20.090 回答