用js實現facebook登錄獲取用戶的信息(包括郵箱)

//要掛代理

<html><body>

<div id="fb-root"></div>
<script>
  // Additional JS functions here

  window.fbAsyncInit = function() {

var facebookId;
var facebookName;
var facebookEmail;
    FB.init({
      appId      : 你網站上的id, // App ID
      channelUrl : '用facebook登錄後返回頁面的URL', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });


  FB.Event.subscribe('auth.authResponseChange', function(response) {

    if (response.status === 'connected') {

//測試返回出來的值

      testAPI();
    } else if (response.status === 'not_authorized') {
      FB.login();
    } else {
      FB.login();
    }
  });

  };


  (function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);

   }(document)); 


  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
//for(var i in response)
//{
//console.log(i+":"+response[i]);
//}
      retrieveProfiles(response['id']);
    });
  }

function retrieveProfiles(id) {
    // 通過Facebook API 查詢用戶信息
    FB.api({ method: 'fql.query', query: "SELECT uid,email, name, pic_square, books, movies, music, current_location FROM user WHERE uid="+id}, function(response) {
for(var i in response)

{

//顯示用戶的信息

for(var j in response[i])
console.log(j+":"+response[i][j]);
}
    });
}
</script>

<body>

//紅色表示獲得網站訪問用戶郵箱的權限

<fb:login-button show-faces="true" width="200" max-rows="1" scope="email,user_checkins"></fb:login-button>
</body></html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章