2011-05-01 71 views
15

我需要使用Javascript獲取Facebook用戶的uid,性別,照片和其他個人資料數據。使用Javascript獲取Facebook用戶個人資料數據

我記得有一種方法獲得用戶對象與.id,.gender,.photo,但我沒有得到一個API調用的副本,我無法在文檔中找到解釋。

如何使用Javascript獲取用戶UID和性別?

感謝

回答

19

更新:我添加了更詳細一點的答案:

首先,你需要調用FB.init並添加應用ID:

FB.init(
{ 
    appId : APP_ID, 
    status : true, // check login status 
    cookie : true, // enable cookies to allow the server to access the session 
    xfbml : true // parse XFBML 
}); 

下一頁,檢查是否有會話打開(即用戶已登錄)

if(FB.getSession() != null) { 

和查詢的細節是:

FB.api('/me', function(response) 
    { 
     alert ("Welcome " + response.name + ": Your UID is " + response.id); 
    }); 
} 

您還需要添加<div id="fb-root"></div>到你的頁面的<body>和負載<script src="http://connect.facebook.net/en_US/all.js"></script>

用戶配置文件picure可以爲http:/graph.facebook.com/USERID/picture/

檢查訪問Graph API參考文件在這裏:http://developers.facebook.com/docs/reference/api/user/

和FB SDK參考在這裏:http://developers.facebook.com/docs/reference/javascript/

+0

@gary添加代表Aleadam – 2013-04-04 13:23:08

+0

謝謝,但此功能已棄用。改用'FB.getLoginStatus'。 – pltvs 2014-06-04 13:17:09

+0

如何獲取用戶地址?我應該使用response.address嗎? – 2015-10-15 05:33:55

20

在2011年被接受的回答中使用的函數FB.getSession()現在(2012)已被棄用並從Facebook JS SDK中刪除。

你仍然可以發現用戶的Facebook ID與getLoginStatus()方法是這樣的:

FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
    alert ("Your UID is " + response.authResponse.userID); 
    } 
}); 

注意:你還是要初始化Facebook的API,把fb-root元素在你的HTML,像Aleadam的解釋接受的答案。

+1

您的解決方案有效,但有一點小錯誤。我使用fb.login()並且你必須使用response.authResponse.userId而不是userID。 還是非常感謝我從一個大麻煩救了我。 upvoted :) – 2013-02-22 14:30:02

+0

奇怪。我的本地測試和Facebook的文檔都使用大寫字母D.請參閱http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/。 – user327961 2013-02-24 20:46:57

+0

現在它真的很奇怪,因爲這裏它不適用於大寫:D – 2013-02-25 05:41:39

相關問題