作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 Facebook 应用程序、一个 Facebook 页面和一个网站。当有人向我的网站添加评论时,我从下面的代码中检索评论文本。之后我想做的是让我的 Facebook 应用程序将相同的评论文本发布到我的 Facebook 页面。
这是目前我网站上的 JavaScript 代码:
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.
});
}); };
最佳答案
经过广泛的搜索,这里是那些正在寻找它的人的答案。请阅读代码中的注释,它们将为您提供更多信息。
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);
}
创建一个(永久的)access_token:
http://php-academy.blogspot.com/2011/04/how-to-post-from-facebook-app-to.html
关于javascript - 从 JavaScript 向 Facebook 发表评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9873608/
我计划在网页上安装 Facebook 评论模块/插件,比方说: http://www.example.com/ 是否可以将用户重定向到网站内的另一个登录页面?假设我想在点击发表评论后将它们发送到这里:
我是一名优秀的程序员,十分优秀!