LogIn with FaceBook API from PHP doens't work

ⅰ亾dé卋堺 提交于 2019-12-24 07:46:16

问题


I'm trying to make a LogIn or register with Facebook from my web but doesn't work, actually some weeks back that work but since the last week it doesn't change the status from the API

in the father file (for give it that name) bucause I'm using MVC with PHPI declare ths script like this

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '300012410734892',
      cookie     : true,
      xfbml      : true,
      version    : 'v3.0'
    });

    FB.AppEvents.logPageView();   

  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "https://connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>

and in the JS file where I send the data to the controller, i make all of this

/*=============================================
FACEBOOK Button
=============================================*/

$(".facebook").click(function(){

    FB.login(function(response){

        validateUser();

    }, {scope: 'public_profile, email'})

})

/*=============================================
Validate the login
=============================================*/

function validateUser(){

    FB.getLoginStatus(function(response){

        statusChangeCallback(response);

    })

}

/*=============================================
VALIDATE THE CHANG OF STATUS IN FACEBOOK
=============================================*/

function statusChangeCallback(response){

    if(response.status === 'connected'){

        testApi();

    }else{
        console.log("response", response);
        swal({
          title: "¡ERROR!",
          text: "¡There was an error logging in with Facebook, try again!",
          type: "error",
          confirmButtonText: "Close",
          closeOnConfirm: false
        },

        function(isConfirm){
            if (isConfirm) {    
                window.location = localStorage.getItem("rutaActual");
            } 
        });

    }

}

/*=============================================
API FACEBOOK
=============================================*/

function testApi(){

    FB.api('/me?fields=id,name,email,picture',function(response){

        if(response.email == null){

            swal({
              title: "¡ERROR!",
              text: "¡To enter the system you must provide the email information!",
              type: "error",
              confirmButtonText: "Close",
              closeOnConfirm: false
            },

            function(isConfirm){
                if (isConfirm) {    
                    window.location = localStorage.getItem("rutaActual");
                } 
            });

        }else{

            var email = response.email;
            var name= response.name;
            var picture= "http://graph.facebook.com/"+response.id+"/picture?type=large";

            var datos = new FormData();
            datos.append("email", email);
            datos.append("name",name);
            datos.append("picture",picture);

            $.ajax({

                url:rutaOculta+"ajax/usuarios.ajax.php",
                method:"POST",
                data:datos,
                cache:false,
                contentType:false,
                processData:false,
                success:function(respuesta){

                    if(respuesta == "ok"){

                        window.location = localStorage.getItem("rutaActual");

                    }else{

                        swal({
                          title: "¡ERROR!",
                          text: "¡The email"+email+" is already registered with a different method to Facebook!",
                          type: "error",
                          confirmButtonText: "Cerrar",
                          closeOnConfirm: false
                        },

                        function(isConfirm){
                            if (isConfirm) {    

                             FB.getLoginStatus(function(response){

                                 if(response.status === 'connected'){     

                                    FB.logout(function(response){

                                        deleteCookie("fblo_300012410734892");

                                        setTimeout(function(){

                                            window.location=rutaOculta+"salir";

                                        },500)

                                    });

                                    function deleteCookie(name){

                                         document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';

                                    }

                                 }

                             })

                            } 
                        });

                    }

                }

            })

        }

    })

}

/*=============================================
CLOSE SESSION FROM FACEBOOK
=============================================*/

$(".salir").click(function(e){

    e.preventDefault();

     FB.getLoginStatus(function(response){

         if(response.status === 'connected'){     

            FB.logout(function(response){

                deleteCookie("fblo_300012410734892");

                console.log("salir");

                setTimeout(function(){

                    window.location=rutaOculta+"salir";

                },500)

            });

            function deleteCookie(name){

                 document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';

            }

         }

     })

})

the problem is that in the statusChangeCallbackit enter direct to the alert of error and in the console show me this

来源:https://stackoverflow.com/questions/51509524/login-with-facebook-api-from-php-doenst-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!