gpt4 book ai didi

javascript - 如何在开发者api2.4中获取facebook好友列表

转载 作者:行者123 更新时间:2023-11-30 16:42:15 29 4
gpt4 key购买 nike

因为我已经搜索了很多东西来使用脚本或 php 来统计 friend 数量。但我无法得到实际结果。大多数结果只会显示这些东西。

自 2014 年 7 月 22 日星期二起,我们现在在对/v2.2/me/friends 的响应中的摘要属性中返回一个 total_count 字段。

但我不知道如何统计好友数。请帮助我,我浪费了一整天的时间来得到这个。

我试过下面的代码来统计我的 friend 数

<!DOCTYPE html>
<html>
<head>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
</head>
<body>
<script>
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
testAPI();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}

// This function is called when someone finishes with the Login
// Button. See the onlogin handler attached to it in the sample
// code below.
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}

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

// Now that we've initialized the JavaScript SDK, we call
// FB.getLoginStatus(). This function gets the state of the
// person visiting this page and can return one of three states to
// the callback you provide. They can be:
//
// 1. Logged into your app ('connected')
// 2. Logged into Facebook, but not your app ('not_authorized')
// 3. Not logged into Facebook and can't tell if they are logged into
// your app or not.
//
// These three cases are handled in the callback function.

FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});

};

// Load the SDK asynchronously
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

// Here we run a very simple test of the Graph API after login is
// successful. See statusChangeCallback() for when this call is made.
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
}
</script>

<!--
Below we include the Login Button social plugin. This button uses
the JavaScript SDK to present a graphical Login button that triggers
the FB.login() function when clicked.
-->

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

<div id="status">
</div>

</body>
</html>

FB.api('/me', function(response) { 它将返回我的用户 ID 和名称,我已经尝试过 FB.api('/me/friends', function(response) { 它返回一个空数组..

请帮我解决这个问题..

最佳答案

为了从版本 2+ 获取好友列表,FB 添加了一个额外的权限 user_friends,您必须要求用户访问他的好友列表。从您上面的代码看来您没有权限。将 user_friends 添加到范围参数。

所以你的登录按钮参数应该是这样的

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

提示:您可以使用以下网址来检查您是否拥有该权限。

GET /{user-id}/permissions 

如果您已经获得许可,并且因为从版本 2+ /me/friends 返回了用户的也在使用您的应用程序的 friend ,并且您的应用程序相对较新,所以您得到的响应是空的您的 friend 可能都没有在使用该应用程序。

您可能想阅读 upgrade guide来自 Facebook 以更好地了解它。

关于javascript - 如何在开发者api2.4中获取facebook好友列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31774585/

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