duanmeng2842 2016-01-28 18:00
浏览 182
已采纳

使用facebook登录获取用户名,电子邮件

I am using login using facebook in my website,

Here is the button

<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>

I am using the exact example given here

But after the check login status, i am getting the response with only the userid of facebook and token, but i want to get the username too.. How can i get that ?

Help pls

Here is the script

window.fbAsyncInit = function () {
        FB.init({
            appId: 'xxxx',
            cookie: true, // enable cookies to allow the server to access 
            // the session
            xfbml: true, // parse social plugins on this page
            version: 'v2.2' // use version 2.2
        });

        FB.getLoginStatus(function (response) {
            console.log(response);
            console.log(response.email);

            //here status is connected
            //statusChangeCallback(response);
        });

    };

In the resopnse, i am getting the entire json,

In the response.status i am getting as connected

But while i try response.email i am getting undefined

How can i get the email or username ?

  • 写回答

1条回答 默认 最新

  • dream5694 2016-02-10 03:38
    关注

    EDITED to show what actually fixed it for me:

    So it does appear to depend on what version of the API your config is set to allow. BUT, I was able to get the email address on a v2.5 api by using the following:

    If you are using the Hello World code, then replace this section:

    FB.api('/me', function (response) {
                            console.log('Success ');
                            console.log(response);
                        });
    

    With this:

    FB.api('/me', { fields: 'email' }, function (response) {
                            console.log('Success ');
                            console.log(response);
                        });
    

    The difference is adding the { fields: 'email' } object to the call. This appears to have been a new addition in either v2.4 or v2.5 of the API which is why older code you see on SO appears to work for the poster but not for you. Hope this helps.


    I'm not sure this qualifies as a solution, but I was able to reproduce this issue (no email returned after a valid request).

    I have two apps, each created under a different FB account. The first one was created more than a year ago and the second one was created today. I have code that runs perfectly against the older app (returns email) and fails against the newer app (no email).

    At first I thought maybe FB just holds back on certain data for brand new apps but I thought it was odd that it wouldnt be documented anywhere.

    So I decided to compare each app configuration side by side. I found that the older one had API VERSION (at the top of the dashboard) set to 2.0 and the newer was 2.5. The Hello World code on the API SDK page requests version 2.2 like so:

    window.fbAsyncInit = function () {
                FB.init({
                    appId: [my app id here],
                    cookie: true,  // enable cookies to allow the server to access 
                    // the session
                    xfbml: true,  // parse social plugins on this page
                    version: 'v2.2' 
                })
            };
    

    According to the documentation, the fact that the newer app has API set to 2.5 in the config means that any requests for an older API (eg 2.2) will be automatically upgraded to 2.5. When I set the javascript above to request 2.5 (eg version: 'v2.5'), I was able to get the older app to fail too. So I suspect the issue is with the newer v2.5 api.

    I have not been able to find out how to set my new app to accept older api calls but will update here if I do.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题