1

ChildBrowser PhoneGap プラグインを使用して Facebook ログインを実行しようとしています。残念ながら、私のコードはうまく機能しません。に到達しますalert("Found redirect location");(そして、実際には何らかの理由でこのアラートを最大8回実行します...)が、FB.getLoginStatus(...)呼び出しから戻ることができません(これ以上アラートが表示されないため、これを知っています)。

このコードの流れはすべて、「Login with 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