科尔多瓦:使用javascript api无法登录facebook

时间:2014-09-15 07:14:04

标签: javascript android facebook cordova

我在我的应用程序中使用JavaScript登录与facebook api。它在网络应用程序中工作正常。但是当我为Android构建应用程序然后在按钮上单击它显示添加Facebook凭据登录的窗口,但在提交凭据时显示白色屏幕和重定向功能不起作用。我是新的cordova应用程序。所以有人可以告诉我在cordova应用程序中使用JavaScript登录facebook是否正确或者还有其他的东西。这是我的代码

HTML部分

 <div id="fb-root"></div>
 <div id="user-info"></div>
 <a href="#" class="f_icon" id='fb-auth'>Sign in with Facebook</a>

facebook的js

  window.fbAsyncInit = function() {
   FB.init({
    appId: '33XXXXXXXXXX',
    status: true,
    cookie: true,
    xfbml: true,
    oauth: true
});

function updateButton(response) {
    var button = document.getElementById('fb-auth');

    if (response.authResponse) {

        FB.api('/me', function(response) {

        });
        button.onclick = function() {
            FB.login(function(response) {
                if (response.authResponse) {
                    FB.api('/me', function(response) {

                        var user_info = {
                            name: response.name,
                            email: response.email,
                            fb_id: response.id,
                            gender: response.gender
                        };

                        $.ajax({
                            url: 'ws/webservice.php?mode=facebook_login',
                            data: user_info,
                            type: "POST",
                            dataType: 'json',
                            success: function(json) {
                                if (json.status == "success") {
                                                                                                      window.localStorage.setItem("session_id",json.session_id);

                  var session_id  = window.localStorage.getItem("session_id");

                                    window.location.href = "dashboard.html";

                                } else {

                                }
                            },
                        });
                    });
                } else {
                    //user cancelled login or did not grant authorization
                }
            }, {
                scope: 'email'
            });
        }
    } else {
        //user is not connected to your app or logged out
        button.innerHTML = 'Sign in with Facebook';
        button.onclick = function() {
            FB.login(function(response) {
                if (response.authResponse) {
                    FB.api('/me', function(response) {

                        var user_info = {
                            name: response.name,
                            email: response.email,
                            link: response.link,
                            fb_id: response.id,
                            gender: response.gender
                        };
                        $.ajax({
                            url: 'webservice.php?mode=facebook_login',
                            data: user_info,
                            type: "POST",
                            dataType: 'json',
                            success: function(json) {
                                if (json.status == "success") {

                        window.localStorage.setItem("session_id", json.session_id);

                  var session_id =  window.localStorage.getItem("session_id");
                   window.location.href = "dashboard.html";

                                } else {

                                }
                            },
                        });


                    });
                } else {
                    //user cancelled login or did not grant authorization
                }
            }, {
                scope: 'email'
            });
        }
    }
}

// run once with current status and whenever the status changes
FB.getLoginStatus(updateButton);
// FB.Event.subscribe('auth.statusChange', updateButton);
};
(function() {
var e = document.createElement('script');
e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());

2 个答案:

答案 0 :(得分:1)

由于cordova不是一个网络应用程序,它没有域名,因此它没有任何页面返回到#34;网站&#34;。要让Facebook连接到cordova,你应该真正了解如何实现插件。

这是适用于Android和iOS的Facebook Connect插件: https://github.com/Wizcorp/phonegap-facebook-plugin

假设您使用了cordova 3.3.0及更高版本。

答案 1 :(得分:1)

我使用此代码 https://github.com/ccoenraets/OpenFB 并在app-browser中解决了我的问题。

这是 https://github.com/krisrak/jquery-cordova-oauth2

上的另一个插件