gpt4 book ai didi

javascript - 使用 JavaScript SDK 访问 Facebook 好友列表失败

转载 作者:行者123 更新时间:2023-11-30 13:24:23 25 4
gpt4 key购买 nike

我在 Stack Overflow 上看到有关访问用户 Facebook 好友列表的问题。我已经尝试过许多不同的方法来做到这一点,不幸的是似乎没有什么对我有用。

任何人都可以告诉我我面临的确切问题吗?我是 javascript 的新手。下面是我使用的代码

    <html>
<head>
<title>My Facebook Login Page</title>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
var friends = new Array();
FB.init({
appId : 'some_id', // App ID
channelUrl : 'http://apps.facebook.com/myapp', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});

FB.api('/me/friends', function(response) {
if(response.data) {
$.each(response.data,function(index,friend) {
alert(friend.name + ' has id:' + friend.id);
});
} else {
alert("Error!");
}
});

};



// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "http://connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>

<p>
<fb:profile-pic uid='loggedinuser' facebook-logo='true'/>
</p>
</br>
</br>
"Welcome, <fb:name uid='' useyou='false'></fb:name>
</br>

</br>
</br>



</br>
</br>

</br></br> <fb:login-button autologoutlink="true" />

</body>
</html>

我收到未定义的响应,甚至无法登录 Facebook。我尝试使用以下示例 http://jobyj.in/api/get-facebook-friends-using-javascript-sdk/ .这在演示中运行良好,但后来我下载了它并尝试从我的机​​器上运行它也没有给我任何结果。

对我有什么建议吗?

更新

我正在使用下面@Somnath 给出的示例并能够登录。但在那之后, response.session 的值未定义导致好友列表为零。对此有什么想法吗?

最佳答案

这是通过javascript登录的教程。

Tutorial Javascript SDK

我尝试登录。否则你会得到未定义的响应。

FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
FB.api('/me/friends', function(response) {
if(response.data) {
$.each(response.data,function(index,friend) {
alert(friend.name + ' has id:' + friend.id);
});
} else {
alert("Error!");
}
});
}
});

您将获得登录用户的好友列表。

更新的答案:不要在初始化时指定 channelUrl。仅给出以下四个参数。试试这个。

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

更新2:尝试使用 fql:

if (response.session) {  

//getting current logged in user's id from session object

globaluserid=response.session["uid"];

//fetching friends uids from 'friend' table. We are using FB.api syntax

FB.api(

{

method: 'fql.query',

query: 'SELECT uid1 FROM friend WHERE uid2='+globaluserid

},

function(response) {

//once we get the response of above select query we are going to parse them

for(i=0;i<response.length;i++)

{

//fetching name and square profile photo of each friends

FB.api(

{

method: 'fql.query',

query: 'SELECT name,pic_square FROM user WHERE uid='+response[i].uid1

},

function(response) {

//creating img tag with src from response and title as friend's name

htmlcontent='<img src='+response[0].pic_square+' title='+response[0].name+' />';

//appending to div based on id. for this line we included jquery

$("#friendslist").append(htmlcontent);

}

);

}

}

);

} else {

// no user session available, someone you dont know

top.location.href="../kfb_login.php";

}

关于javascript - 使用 JavaScript SDK 访问 Facebook 好友列表失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9029622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com