我一直在阅读developers.facebook.com网站,但他们解释这些事情非常糟糕。所以我希望你们中的一些人可以向我解释如何使用JavaScript和Graph API在Facebook状态上添加评论和按钮。
我现在拥有的是一个显示登录用户主页提要的应用程序(如果需要,可以显示新闻源)。我想要的是基本的Facebook功能,即能够评论和喜欢这些状态,图像,股票等(就像你在Facebook上一样),还可以看到评论和喜欢这些照片和状态的其他人。
我知道你需要使用POST方法,但我不知道我会这样做(这里有一些信息:http://developers.facebook.com/docs/reference/api/publishing/)
所以这是我现在的代码(我不知道这是否是最有效的方法,但它确实有效。如果你能给我一些有建设性的批评,我会很高兴的):
FB.api('/me/home', function(response) {
console.log(response.data);
for(var i=0, len = response.data.length; i < len; i++){
var post_type = response.data[i].status_type; //Post type
var poster_id = response.data[i].from.id; //Poster
var poster_name = response.data[i].from.name; //Posters name
var poster = '<a href="http://www.facebook.com/' + poster_id + '/" >' + poster_name + '</a>'; //Link to poster
var poster_pic = 'https://graph.facebook.com/' + poster_id + '/picture'; //Posters profile pic
var poster_img = '<img height="50px" width="50px" src="' + poster_pic + '" />';
var post_id = response.data[i].id; //Id of message
var post = response.data[i].message; //Content of message
var post_date = response.data[i].created_time; //Date created
var object_id = response.data[i].object_id; //Pic id (if pic's an object)
var post_img_url = response.data[i].picture; //Pic url
if(!object_id) {
var post_img = '<a href="http://www.facebook.com/' + post_id + '/"> <img src="' + post_img_url + '" /> </a>'; // <a href="http://www.facebook.com/' + post_id + '/">See at Facebook</a>
} else {
var post_img = '<a href="http://www.facebook.com/' + object_id + '/"> <img src="' + post_img_url + '" /> </a>';
}
if (!post_img_url) {
post_img = '';
}
if(!post) {
post = response.data[i].story;
}
if (post_type === 'wall_post') {
if(response.data[i].to) {
var posted_to_name = response.data[i].to.data[0].name;
var posted_to_id = response.data[i].to.data[0].id;
var posted_to = '<a href="http://www.facebook.com/' + posted_to_id + '/" >' + posted_to_name + '</a>';
document.getElementById("write").innerHTML += ('<hr> <div class="facebook_content"> <div class="facebook_left_side"> <div class="facebook_content_header"> <div class="poster_img">' + poster_img + '</div> <div class="poster">' + poster + '</div> <div class="post_type"> wrote on ' + posted_to + '\'s wall: </div> <div class="post_date">' + post_date + '</div> </div> <div class="story">' + post + '</div> </div> <div class="facebook_right_side"> <div class="img">' + post_img + '</div> </div> </div> </br>');
} else {
document.getElementById("write").innerHTML += ('<hr> <div class="facebook_content"> <div class="facebook_left_side"> <div class="facebook_content_header"> <div class="poster_img">' + poster_img + '</div> <div class="poster">' + poster + '</div> <div class="post_date">' + post_date + '</div> </div> <div class="story">' + post + '</div> </div> <div class="facebook_right_side"> <div class="img">' + post_img + '</div> </div> </div> </br>');
}
} else if (post_type === 'added_photos') {
document.getElementById("write").innerHTML += ('<hr> <div class="facebook_content"> <div class="facebook_left_side"> <div class="facebook_content_header"> <div class="poster_img">' + poster_img + '</div> <div class="poster">' + poster + '</div> <div class="post_type"> added a photo: </div> <div class="post_date">' + post_date + '</div> </div> <div class="story">' + post + '</div> </div> <div class="facebook_right_side"> <div class="img">' + post_img + '</div> </div> </div> </br>');
} else if (post_type === 'shared_story') {
document.getElementById("write").innerHTML += ('<hr> <div class="facebook_content"> <div class="facebook_left_side"> <div class="facebook_content_header"> <div class="poster_img">' + poster_img + '</div> <div class="poster">' + poster + '</div> <div class="post_type"> shared: </div> <div class="post_date">' + post_date + '</div> </div> <div class="story">' + post + '</div> </div> <div class="facebook_right_side"> <div class="img">' + post_img + '</div> </div> </div> </br>');
} else {
document.getElementById("write").innerHTML += ('<hr> <div class="facebook_content"> <div class="facebook_left_side"> <div class="facebook_content_header"> <div class="poster_img">' + poster_img + '</div> <div class="poster">' + poster + '</div> <div class="post_date">' + post_date + '</div> </div> <div class="story">' + post + '</div> </div> <div class="facebook_right_side"> <div class="img">' + post_img + '</div> </div> </div> </br>');
}
}
});
我显然需要添加三个(或更多)div,其中包含like和comment按钮,并且至少有一个在单击时显示最新的注释(就像在Facebook上一样)。我需要添加某种JS代码,它向facebook发送一个请求,其中包含状态/图像的id,用户的id,执行的动作类型(如或评论)以及最后的内容在这种情况下评论。
我要求提供这些信息的原因是我正在研究一个项目,我只是尝试使用Facebook Graph API的不同功能,因为它们可以很好地补充我的项目功能,而且它是很高兴知道以后的参考。
希望你们中的一些人知道我能做些什么才能让它发挥作用
此致 亚历山大。
答案 0 :(得分:1)
就个人而言,我认为Docs做得很好。
来自docs:
Create
You can write to the POST_ID/comments connection to post a comment to the post by issuing an HTTP POST request with the publish_stream permission and following parameters.
Parameter Description Type Required
message Comment text string yes
评论很简单,一旦您拥有要评论的帖子的ID,只需运行
即可 var comment = 'Reading JS SDK documentation';
var POST_ID = '1231412351234';
FB.api('/'+POST_ID+'/comments', 'post', { message: comment }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Great! It worked!');
}
});
同样,喜欢甚至更简单。来自相同的文档:
You can like a Post by issuing a HTTP POST request to the POST_ID/likes connection with the publish_stream permission. You can suppress the notification created when liking a Post by passing a notify parameter with value of false.
所以,在Javascript中:
var POST_ID = '1231412351234';
FB.api('/'+POST_ID+'/likes', 'post', function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Great! It worked!');
}
});