【问题标题】:Get facebook user profile using cordova使用cordova获取facebook用户资料
【发布时间】:2016-04-21 09:06:57
【问题描述】:

我想在用户使用 facebook 登录时获取名字、姓氏、电子邮件。我的应用程序在科尔多瓦。我正在使用cordova插件进行facebook身份验证这个“https://github.com/Wizcorp/phonegap-facebook-plugin”。我可以使用 Facebook 登录,但我怎样才能从中获取姓名和电子邮件。 提前致谢。

【问题讨论】:

    标签: facebook cordova cordova-plugins


    【解决方案1】:

    使用下面的命令See Details添加 facebook cordova 插件

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

    html代码

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

    Javascript 代码

    <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>
    

    【讨论】:

    • 非常感谢@kishore。真的很棒!!
    【解决方案2】:

    试试下面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))
            });
    }
    

    【讨论】:

    • 非常感谢.. 杰伊
    • @Krishnavrinsoft 你有带此代码的电子邮件、名字、姓氏吗?
    • @Krishnavrinsoft 请支持它,因为它正在工作。因为我已经给出了第二个答案,所以谢谢:)。
    • 是的,我从这段代码中收到了电子邮件和名字。我怎样才能得到姓氏?
    • @Krishnavrinsoft 检查这里你会得到这个this.lastname = result.last_name; 的姓氏。检查它会给你所有需要的信息。
    猜你喜欢
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多