Get facebook user profile using cordova

前端 未结 2 1620
时光取名叫无心
时光取名叫无心 2020-12-20 00:40

I want to get first name, last name, email when user login with facebook. My app is in Cordova. I am using cordova plugin for facebook authentication this one \"https://gith

相关标签:
2条回答
  • 2020-12-20 01:06

    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>
    
    0 讨论(0)
  • 2020-12-20 01:14

    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))
            });
    }
    
    0 讨论(0)
提交回复
热议问题