5

我正在开发一个带有 facebook 集成的 android phonegap 应用程序。FB.login(function(response) 函数有一个响应处理程序,当用户单击 fb 按钮时它会被调用,但它不会每次都进入函数响应或 alert('test')。只有当我进入这个脚本时击中大约 5 或 6 次。我需要的是在我第一次登录 facebook 时获取访问令牌。我已经浏览了很多关于此的链接,但无法找到确切的解决方案...

如果用户已经登录 facebook,则 FB 登录回调函数没有响应

即使这个问题似乎是相同的,但我无法弄清楚我的解决方案。

这是正在工作的代码:

<div id="data">Hello Facebooktesters, loading ...</div>

<button onclick="login()">Login</button>
<button onclick="me()">Me</button>
<button onclick="logout()">Logout</button>
<button onclick="Post()">facebookWallPost</button>

<script type="text/javascript">
    document.addEventListener('deviceready', function() {
        try {
            alert('Device is ready! Make sure you set your app_id below this alert.');
            FB.init({
                appId : "xxxxxxxxxxxxxxx",
                nativeInterface : CDV.FB,
                useCachedDialogs : false
            });
            document.getElementById('data').innerHTML = "FB init executed";
        } catch (e) {
            alert(e);
        }
    }, false);

    function me() {
        FB.api('/me/friends', {
            fields : 'id, name, picture'
        }, function(response) {
            if (response.error) {
                alert(JSON.stringify(response.error));
            } else {
                var data = document.getElementById('data');
                fdata = response.data;
                console.log("fdata: " + fdata);
                response.data.forEach(function(item) {
                    var d = document.createElement('div');
                    d.innerHTML = "<img src="+item.picture+"/>" + item.name;
                    data.appendChild(d);
                });
            }
            var friends = response.data;
            console.log(friends.length);
            for ( var k = 0; k < friends.length && k < 200; k++) {
                var friend = friends[k];
                var index = 1;

                friendIDs[k] = friend.id;
                //friendsInfo[k] = friend;
            }
            console.log("friendId's: " + friendIDs);
        });
    }

    function login() {
        FB.login(function(response) {
            alert('test');
            if (response.authResponse) {
                alert('logged in');
                var access_token =   FB.getAuthResponse()['accessToken'];
                alert(access_token);
                 window.location = "test.html"
            } else {
                alert('not logged in');
            }
        }, {
            scope : "email"
        });
    }

    function logout() {
        alert('test');
          FB.logout(function(response) {
              alert('logged out');
            //window.location.reload();
          });
    }

    function Post(ele) {
        var domain = 'http://192.168.0.46:8082/';
        console.log('Debug 1');
        var params = {
        method: 'feed',
        name: 'test - test',
        link: domain+'test/test/showproddetails.action?product.productId=1',
        picture: 'http://www.careersolutions.com/test.png',
        caption: 'test',
        description: 'test'
        };
        console.log(params);
        FB.ui(params, function(obj) { console.log(obj);});
    }

</script>

谢谢你,拉杰

4

2 回答 2

0

您在此行中缺少分号:

window.location = "test.html"

于 2012-06-19T06:07:41.717 回答
0

这个问题是不久前发布的,但我想指出一些事情。FB.login函数会打开一个弹出对话框,询问用户是否愿意使用 Facebook 登录您的应用程序。通常建议在单击按钮后运行它,因为大多数浏览器会在页面加载时阻止弹出对话框。如果用户之前已授权您的应用程序,则弹出对话框将重定向回您的应用程序并关闭对话框(假设您的应用程序是一个网站)。

如果您打算在页面加载时检查用户是否登录到您的应用程序,那么建议您改用FB.getLoginStatus这样的方法

function checkLoginState() {
   FB.getLoginStatus(function(response) {
      // Check response.status to see if user is logged in
      alert(response.status);
   });
}

请参阅facebook 上的大量文档

于 2016-05-07T19:29:28.390 回答