解析Facebook登录无法在Ionic View中

时间:2015-11-08 23:14:26

标签: javascript facebook ionic

我试图让Parse.FacebookUtils.logIn在Ionic视图上工作,我在浏览器中点击离子服务时已成功地使其工作,但问题是当我尝试使用登录时Ionic View仿真器,基本上认证Facebook窗口不会弹出,我不知道为什么。

我遵循了这个文档: https://www.parse.com/docs/js/guide#users-facebook-users

浏览器中的所有功能都非常有用,只是试图让它在Ionic视图中工作。

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

我放弃了使用Ionic的Parse sdk并开始使用另一个插件使用下面的文档:

http://ngcordova.com/docs/plugins/oauth/

https://github.com/nraboy/ng-cordova-facebook-example/blob/master/www/js/app.js

以下是我使用Parse工作的代码:

$scope.fbLogin = function() {
            var result;
            $cordovaOauth.facebook('987971754588010', ['public_profile, user_birthday, email']).then(function(success) {

                $http.get("https://graph.facebook.com/v2.2/me", { params: { access_token: success.access_token, fields: "id,name,birthday,last_name,first_name,email,gender,picture.type(large)", format: "json" }}).then(function(result) {

                    $localstorage.setObject('fbData', result.data);

                    var expiration_date = new Date();
                    expiration_date.setSeconds(expiration_date.getSeconds() + success.expires_in);
                    expiration_date = expiration_date.toISOString();

                    var facebookAuthData = {
                        "id": result.data.id,
                        "access_token": success.access_token,
                        "expiration_date": expiration_date
                    };

                    Parse.FacebookUtils.logIn(facebookAuthData, {
                        success: function(user) {
                            if (!user.existed()) {
                                alert("User signed up and logged in through Facebook!");
                                $scope.openModal();
                            } else {
                                alert("User logged in through Facebook!");
                                $scope.syncFbData(null);
                            }
                        },
                        error: function(user, error) {
                            console.log('ERROR: ' + error);
                            var alertPopup = $ionicPopup.alert({
                                title: 'Error de autenticación de usuario',
                                template: 'El usuario canceló autenticación con Facebook o no autorizó permisos. '
                            });
                        }
                    });
                }, function(error) {
                    console.log('ERROR: ' + error);
                    var alertPopup = $ionicPopup.alert({
                        title: 'Error de conexión',
                        template: 'No se ha logrado establecer conexión con Facebook.'
                    });
                });
            }, function(error) {
                console.log('ERROR: ' + error);
                var alertPopup = $ionicPopup.alert({
                    title: 'Error de conexión',
                    template: 'No se ha logrado establecer conexión con Facebook.'
                });
            });
        };

        $scope.syncFbData = function (newUsername) {
            var fbData = $localstorage.getObject('fbData')
            var currentUser = Parse.User.current();
            if (newUsername) {
                currentUser.set('username', newUsername);
            }
            currentUser.set('nombre', fbData.first_name);
            currentUser.set('apellidos', fbData.last_name);
            currentUser.set('email',fbData.email);
            currentUser.set('fechaNacimiento', new Date(fbData.birthday));
            currentUser.set('imagenUrl',fbData.picture.data.url);
            currentUser.set('genero',((fbData.gender === 'male') ? 'Hombre' : 'Mujer') );
            currentUser.save(null, {
                success: function (response) {
                    $scope.closeModal();
                    $state.go('app.lista-cervezas');
                },
                error: function (response, error) {
                    console.log('ERROR: ' + error);
                    var alertPopup = $ionicPopup.alert({
                        title: 'Nombre de usuario invalido',
                        template: 'El nombre de usuario ingresado ya se encuentra en uso!'
                    });
                }
            });
        };