使用Facebook Javascript API检索用户信息(即位置,电子邮件)

时间:2016-06-20 22:39:47

标签: javascript facebook facebook-graph-api facebook-javascript-sdk

我正在使用Facebook Javascript SDK来实现Facebook登录。我正在尝试获取姓名,电子邮件,位置,喜欢,朋友等,并将用户重定向到登机。

我能够获取accessToken和userID值;但是,我正在尝试获取我专门放在我的范围内的其他用户信息。我试过看Facebook returns undefined for email and birthday 和其他问题,但没有运气

function fb_login(){
    FB.login(function(response) {
        if (response.authResponse) {
            console.log('Welcome!  Fetching your information.... ');
            console.log(response); // dump complete info
            var accessToken =   FB.getAuthResponse()['accessToken'];
            console.log('Access Token = '+ accessToken);
            var userID = FB.getAuthResponse()['userID']; //get FB UID
            console.log("userID: "+ userID);
            //refer to getData() function below
            getData();
        } else {
            //user hit cancel button
            console.log('User cancelled login or did not fully authorize.');
        }
    }, {scope: 'public_profile,email,user_friends,user_likes,user_location'});
}

function getData() {
  FB.api('/me', function(response) {
    console.log("email: "+response.email);
    // you can store this data into your database
    console.log('Good to see you, ' + response.name + '.');
      //check if the loginStatus works
      FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
          // the user is logged in and has authenticated your
          // app, and response.authResponse supplies
          // the user's ID, a valid access token, a signed
          // request, and the time the access token 
          // and signed request each expire

          //redirect to start/location.ejs
          //window.location = "start/location";

        } else if (response.status === 'not_authorized') {
          // the user is logged in to Facebook, 
          // but has not authenticated your app
        } else {
          // the user isn't logged in to Facebook.
        }
      });
  });
}

这是为什么?

1 个答案:

答案 0 :(得分:1)

我能够弄清楚。不得不定义一些字段......现在我必须弄清楚如何从用户那里获得更大的个人资料图片

function fb_login(){
    FB.login(function(response) {
        if (response.authResponse) {
            console.log('Welcome!  Fetching your information.... ');
            console.log(response); // dump complete info
            var accessToken =   FB.getAuthResponse()['accessToken'];
            console.log('Access Token = '+ accessToken);
            var userID = FB.getAuthResponse()['userID']; //get FB UID
            console.log("userID: "+ userID);
            //refer to getData() function below
            getData(accessToken);
        } else {
            //user hit cancel button
            console.log('User cancelled login or did not fully authorize.');
        }
    }, {'scope': 'public_profile,email,user_friends,user_likes,user_location'});
}

function getData(accessToken) {
  FB.api('/me', 'get', { access_token: accessToken, fields: 'id,name,gender,email,location,friends,likes,picture' }, function(response) {
    console.log(response);
});
  //check if the loginStatus works
  FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
      // the user is logged in and has authenticated your
      // app, and response.authResponse supplies
      // the user's ID, a valid access token, a signed
      // request, and the time the access token 
      // and signed request each expire

      //redirect to start/location.ejs
      //window.location = "start/location";

    } else if (response.status === 'not_authorized') {
      // the user is logged in to Facebook, 
      // but has not authenticated your app
    } else {
      // the user isn't logged in to Facebook.
    }
  });
}