1

我正在尝试使用 ChildBrowser PhoneGap 插件执行 Facebook 登录。不幸的是,我的代码不太好用。它到达alert("Found redirect location");(并且由于某种原因实际上运行此警报〜8x ...),但未能从FB.getLoginStatus(...)呼叫中返回(我知道这一点,因为我没有收到进一步的警报)。

这段代码的流程都是从单击“使用 Facebook 登录”按钮开始的,导致调用gotoFBLogin()(如果您不熟悉,这是 Backbone)。

    gotoFBLogin : function() {
        var self = this;

        /* On Facebook Login */
        var my_client_id    = "XXXXXXXXXXXXXXXXX",
            my_redirect_uri = "http://mysite.com/login/fb",
            my_type         = "user_agent",
            my_display      = "touch";

        var authorize_url  = "https://graph.facebook.com/oauth/authorize?";
        authorize_url += "client_id="+my_client_id;
        authorize_url += "&redirect_uri="+my_redirect_uri;
        authorize_url += "&display="+my_display;
        authorize_url += "&scope=" + this.FB_PERMISSIONS;

        // show ChildBrowser
        if(IS_MOBILE) {
            // install ChildBrowser (if necessary)
            if(window.plugins.childBrowser == null) {
                ChildBrowser.install();
            }

            window.plugins.childBrowser.onLocationChange = function(loc){
                if (/login/.test(loc)) {
                    alert("Found redirect location"); // THIS RUNS
                    window.plugins.childBrowser.close();
                    self.validateFBLogin();
                }
            };

            window.plugins.childBrowser.showWebPage(authorize_url);
        }
    },

    validateFBLogin : function() {
        var self = this;

        FB.getLoginStatus(function(response) {
            alert("Got response");
            if(response.status == "connected") {
                alert("Connected");
                // build authData object for Parse
                var id = response.authResponse.userID;
                var access_token = response.authResponse.accessToken;
                var expiration_date = new Date();
                expiration_date.setSeconds(expiration_date.getSeconds() + response.authResponse.expiresIn);
                expiration_date = expiration_date.toISOString();
                var authData = {
                    "id" : id,
                    "access_token" : access_token,
                    "expiration_date" : expiration_date
                };

                // log in with Parse
                Parse.FacebookUtils.logIn(authData, {
                    success: self._fbLoginSuccess,
                    error: self._fbLoginError
                });
            }
        }, self.FB_PERMISSIONS);
    },

    _fbLoginSuccess : function(user) {
        $.mobile.loading( 'hide' );

        if(IS_DEBUG) alert("Facebook login succeeded");

        // if the user didn't exist, delete them
        if(!user.existed()) {
            // delete the user
            user.destroy();

            // log them out
            Parse.User.logOut();

            MobileHTMLHelper.showPopup('Hmm... You\'re not signed up. If you request an invite, we\'ll get you in as soon as possible :) Thanks.');
        } else {
            // log it
            Log.add(Log.ACTION_LOGIN_FACEBOOK);

            // Handle successful login
            Backbone.history.navigate('dashboard', {trigger: true});
        }
    },
    _fbLoginError : function(user, error) {
        // Handle errors and cancellation
        $.mobile.loading( 'hide' );

        if(IS_DEBUG) alert("Facebook login failed");
    },

关于我的代码为什么失败的任何想法?谢谢 - 我真的很感激。

4

0 回答 0