Get facebook user profile using cordova

你说的曾经没有我的故事 提交于 2019-11-29 12:43:48

add facebook cordova plugin using below command See Details

cordova plugin add https://github.com/Wizcorp/phonegap-facebook-plugin --variable APP_ID="YOURAPPID" --variable APP_NAME="YOURAPPNAME" 

html code

 <div onclick="fblogin();"><img src="img/facebook.png" /></div>

Javascript code

<script>
            function fblogin(){

            facebookConnectPlugin.login( ["public_profile","email"],
                function (response) {

                    if(response.authResponse.userID!=''){
                        facebookConnectPlugin.api(response.authResponse.userID+"/?fields=id,email,first_name", ["public_profile"],
                        function (response) {
                            console.log('SUCCESS:'+response);
                            alert('first_name : '+response.first_name+',email:'+response.email+',id:'+response.id);
                        },
                        function (response) {
                            console.log('ERROR:'+response);
                        });
                    }    

                }, 
                function (response) {   
                        console.log('ERROR:'+response);
               });

               }

               </script>

Try this Login Function with below Snippet.

var fbLogin = function() {
    facebookConnectPlugin.login(["public_profile", "email", "user_birthday", "user_location"],
        function(response) {
            console.log("Login Response :" + JSON.stringify(response));
            //alert("Login Response :" + JSON.stringify(response))
            this.authId = response.authResponse.userID;
            if (response.status == "connected") {
                facebookConnectPlugin.api("/" + response.authResponse.userID, ["public_profile", "email", "user_birthday", "user_location"],
                    function(result) {
                        this.email = result.email;
                        this.firstname = result.first_name;
                        this.lastname = result.last_name;
                        this.birthdate = result.birthday;
                        this.city = result.location.name;
                    },
                    function(error) {
                        alert("Failed: " + error);
                    });
            }
        },
        function(response) {
            alert("Other Response : " + JSON.stringify(response))
        });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!