我已经在网页上放了最后3个帖子,如果用户在他可以添加评论/喜欢,问题是如果用户插入并尝试添加喜欢或评论他正在获得权限错误#200,如果我在我可以添加喜欢或评论(应用程序是给我的),我得到了用户的所有权限,所以我如何允许他添加喜欢/评论。
代码:
$facebook = new Facebook(array(
'appId' => '',
'secret' => '',
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
if (session_id()) {
} else {
session_start();
}
$access_token = $facebook->getAccessToken();
//check permissions list
$permissions_list = $facebook->api(
'/me/permissions', 'GET', array(
'access_token' => $access_token
)
);
//check if the permissions we need have been allowed by the user
//if not then redirect them again to facebook's permissions page
$permissions_needed = array('publish_stream', 'read_stream', 'manage_pages');
foreach ($permissions_needed as $perm) {
if (!isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1) {
$login_url_params = array(
'scope' => 'publish_stream,read_stream,manage_pages',
'fbconnect' => 1,
'display' => "page",
'redirect_uri' => 'http://localhost/fb/index.php',
);
$login_url = $facebook->getLoginUrl($login_url_params);
header("Location: {$login_url}");
exit();
}
}
}else {
//if not, let's redirect to the ALLOW page so we can get access
//Create a login URL using the Facebook library's getLoginUrl() method
$login_url_params = array(
'scope' => 'publish_stream,read_stream,manage_pages',
'fbconnect' => 1,
'display' => "page",
'redirect_uri'=>'http://localhost/fb/index.php',
);
$login_url = $facebook->getLoginUrl($login_url_params);
//redirect to the login URL on facebook
header("Location: {$login_url}");
exit();
}
$logoutUrl = $facebook->getLogoutUrl();
,如果用户点击了喜欢按钮:
jQuery('ul.fb_list a').click(function(){
var comm_id = jQuery(this).attr("class");
jQuery.post('https://graph.facebook.com/'+comm_id+'/likes/',{
access_token : "<?php echo $access_token ?>"
});
});
答案 0 :(得分:0)
仔细检查并确保您的变量comm_id与发布帖子的用户ID以及帖子本身的id一起格式化,中间有下划线,如下所示 - USERID_POSTID。如果你打电话给图表api
,这就是facebook给你的方式$post_url = "https://graph.facebook.com/" . $user_id . "/posts?access_token=". urlencode($access_token);
不确定您是否从其他来源获取comm_id。在您的代码中也注意到了
access_token : "<?php echo $access_token ?>"
你在回音电话后忘记了一个分号。应该是这个
access_token : "<?php echo $access_token; ?>"
希望这会有所帮助。我看到你正在使用很多jquery来做类似的功能。对于像这样的东西,我喜欢用服务器端代码,我觉得它因某些原因更稳定。
这就是我做的。我有一个像这样的按钮 -
echo '<div id="like_container-'.$i.'">
<div id="like_count">'.$num_likes.'</div>'
<a href="javascript:void()" onclick="fb_Like(\''.$post_id.'\',\''.$access_token.'\','.$num_likes.','.$i.')"><div class="unliked"></div></a>
</div>';
和fb_Like()是一个ajax调用,类似这样 -
function fb_Like(post_id, token, num_likes, id){
$.ajax({
type: "GET",
url: "likepost.php",
data: 'id='+post_id+'&token='+token+'&likes='+num_likes,
success: function(html)
{
$("#like_container-"+id).empty().html(html);
}
});
}
likepost.php页面是一个类似于本页面的脚本
Like a Facebook Post externally using Graph Api - example
这对我来说非常有用。如果有类似的话,我也可以让我更新帖子在同一个按钮正上方的前端有喜欢的数量。祝你好运!
更新
如果你想检查一下用户是否已经喜欢帖子,那就非常简单了/ facebook图表api
//Create Post Url
$post_url = "https://graph.facebook.com/" . $Page/User_id . "/posts?access_token=". urlencode($access_token);
//Get Json Contents
$resp = file_get_contents($post_url,0,null,null);
//Store Post Entries as Array
$the_posts = json_decode($resp, true);
foreach ($the_posts['data'] as $postdata) {
foreach ($postdata['likes']['data'] as $like){
if($like['id']==$user){
$liked=1;
}else{continue;}
}
if($liked==1){
//do something
}
}
这假设您已经拥有登录用户的facebook用户ID,在此示例中,存储在变量$ user中。