从JavaScript向Facebook发表评论 [英] Posting comment to Facebook from JavaScript
本文介绍了从JavaScript向Facebook发表评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我在我的网站上的JavaScript代码:
window.fbAsyncInit = function(){
FB.init({
appId: drupal_to_js($ appid),
status:true,
cookie:true,
xfbml:true,
channelUrl:。drupal_to_js($ channel_url) b});
FB.Event.subscribe('comment.create',function(response){
var commentQuery = FB.Data.query('SELECT text FROM comment WHERE post_fbid = \'' + response.commentID +'\'AND object_id IN(SELECT comments_fbid FROM link_stat WHERE url = \''+ response.href +'\')');
FB.Data.waitOn ([commentQuery],function(){
var commentRow = commentQuery.value [0];
var commentText = commentRow.text;
// TODO发表评论文字到Facebook
});
}); };
解决方案
经过广泛的搜索,这里是答案谁在寻找它请阅读代码中的意见,他们会给你更多的信息。
window.fbAsyncInit = function(){
FB.init({
appId:。drupal_to_js($ appid)。,
status:true,
cookie:true,
xfbml:true,
channelUrl:。drupal_to_js($ channel_url)。
});
FB.Event.subscribe('comment.create',function(response){//创建注释时触发
var commentQuery = FB.Data.query('SELECT fromid,text FROM comment WHERE post_fbid = \''+ response.commentID +'\'AND object_id IN(SELECT comments_fbid FROM link_stat WHERE url = \''+ response.href +'\')');
var userQuery = FB.Data.query('SELECT name FROM user WHERE uid in(select fromid from {0})',commentQuery);
FB.Data.waitOn([commentQuery,userQuery], function(){
var commentRow = commentQuery.value [0];
var userRow = userQuery.value [0];
var commentText = commentRow.text;
var commentUsername = userRow.name;
//调用此函数向Facebook发送Ajax请求
post_to_fb(commentText,commentUsername,response.href);
}) ;
}); };
//此功能将发布到具有ID $ fb_page_id的Facebook页面。
//帖子格式是这样的,[NAME说:] POST例如:[ObyYou说:]这是一个测试。
//当然,您可以按照所需的方式更改格式。
//您必须拥有访问权限才能在该页面上发布权限。
//使用这个答案底部的两个站点来帮助(记住,如果你要硬编码访问令牌,你必须创建一个永久访问令牌)。
//注意一些变量和函数是PHP,因为我的JavaScript代码是
//实际上在一个PHP文件中。
function post_to_fb(commentText,commentUsername,commentLink){
var strURL ='https://graph.facebook.com/。$ fb_page_id。/ feed';
var params ='link ='+ commentLink +'& message = ['+ commentUsername +'+说:] +'+ commentText +'& access_token =$ fb_page_access_token。
var xmlHttpReq;
xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open('POST',strURL,true);
xmlHttpReq.setRequestHeader('Content-type','application / x-www-form-urlencoded');
xmlHttpReq.send(params);
}
创建一个(永久)access_token:
I have a Facebook application, a Facebook page and a website. When someone adds a comment to my website, I retrieve the comment text from the code below. What I want to do after that is to let my Facebook application post the same comment text to my Facebook page.
This is the JavaScript code I have so far on my website:
window.fbAsyncInit = function() {
FB.init({
appId: " . drupal_to_js($appid) . ",
status: true,
cookie: true,
xfbml: true,
channelUrl: " . drupal_to_js($channel_url) . "
});
FB.Event.subscribe('comment.create', function(response) {
var commentQuery = FB.Data.query('SELECT text FROM comment WHERE post_fbid=\'' + response.commentID + '\' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url=\'' + response.href + '\')');
FB.Data.waitOn([commentQuery], function () {
var commentRow = commentQuery.value[0];
var commentText = commentRow.text;
//TODO Post commentText to the Facebook page.
});
}); };
解决方案
After an extensive search, here is the answer for those who are looking for it. Please read the comments inside the code, they will give you more information.
window.fbAsyncInit = function() {
FB.init({
appId: " . drupal_to_js($appid) . ",
status: true,
cookie: true,
xfbml: true,
channelUrl: " . drupal_to_js($channel_url) . "
});
FB.Event.subscribe('comment.create', function(response) { //trigger when comment is created
var commentQuery = FB.Data.query('SELECT fromid, text FROM comment WHERE post_fbid=\'' + response.commentID + '\' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url=\'' + response.href + '\')');
var userQuery = FB.Data.query('SELECT name FROM user WHERE uid in (select fromid from {0})', commentQuery);
FB.Data.waitOn([commentQuery, userQuery], function () {
var commentRow = commentQuery.value[0];
var userRow = userQuery.value[0];
var commentText = commentRow.text;
var commentUsername = userRow.name;
//Call this function to send an Ajax request to Facebook.
post_to_fb(commentText, commentUsername, response.href);
});
}); };
//This function will post to a Facebook page that has the ID $fb_page_id.
//The post format is like this, [NAME said:] POST e.g: [ObyYou said:] this is a test.
//Of course, you can change the format the way you want.
//You have to have an access key to have the permission to post on that page.
//Use the two sites at the bottom of this answer for help, (remember if you
//want to hard-code the access token, you have to create a permenant access token).
//Note that some variables and functions are PHP, since my JavaScript code is
//actually inside a PHP file.
function post_to_fb(commentText, commentUsername, commentLink) {
var strURL = 'https://graph.facebook.com/" . $fb_page_id . "/feed';
var params = 'link=' + commentLink + '&message=[' + commentUsername +'+said:]+' + commentText + '&access_token=" . $fb_page_access_token . "';
var xmlHttpReq;
xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open('POST', strURL, true);
xmlHttpReq.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlHttpReq.send(params);
}
Create A (permanent) access_token:
http://php-academy.blogspot.com/2011/04/how-to-post-from-facebook-app-to.html
这篇关于从JavaScript向Facebook发表评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文