admin管理员组文章数量:1395905
I have a Facebook application, a Facebook page and a website. When someone adds a ment to my website, I retrieve the ment text from the code below. What I want to do after that is to let my Facebook application post the same ment 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('ment.create', function(response) {
var mentQuery = FB.Data.query('SELECT text FROM ment WHERE post_fbid=\'' + responsementID + '\' AND object_id IN (SELECT ments_fbid FROM link_stat WHERE url=\'' + response.href + '\')');
FB.Data.waitOn([mentQuery], function () {
var mentRow = mentQuery.value[0];
var mentText = mentRow.text;
//TODO Post mentText to the Facebook page.
});
}); };
I have a Facebook application, a Facebook page and a website. When someone adds a ment to my website, I retrieve the ment text from the code below. What I want to do after that is to let my Facebook application post the same ment 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('ment.create', function(response) {
var mentQuery = FB.Data.query('SELECT text FROM ment WHERE post_fbid=\'' + response.mentID + '\' AND object_id IN (SELECT ments_fbid FROM link_stat WHERE url=\'' + response.href + '\')');
FB.Data.waitOn([mentQuery], function () {
var mentRow = mentQuery.value[0];
var mentText = mentRow.text;
//TODO Post mentText to the Facebook page.
});
}); };
Share
edited Apr 9, 2012 at 8:38
Peter Mortensen
31.6k22 gold badges110 silver badges133 bronze badges
asked Mar 26, 2012 at 14:04
obadaobada
1813 silver badges13 bronze badges
1 Answer
Reset to default 4After an extensive search, here is the answer for those who are looking for it. Please read the ments 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('ment.create', function(response) { //trigger when ment is created
var mentQuery = FB.Data.query('SELECT fromid, text FROM ment WHERE post_fbid=\'' + response.mentID + '\' AND object_id IN (SELECT ments_fbid FROM link_stat WHERE url=\'' + response.href + '\')');
var userQuery = FB.Data.query('SELECT name FROM user WHERE uid in (select fromid from {0})', mentQuery);
FB.Data.waitOn([mentQuery, userQuery], function () {
var mentRow = mentQuery.value[0];
var userRow = userQuery.value[0];
var mentText = mentRow.text;
var mentUsername = userRow.name;
//Call this function to send an Ajax request to Facebook.
post_to_fb(mentText, mentUsername, 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(mentText, mentUsername, mentLink) {
var strURL = 'https://graph.facebook./" . $fb_page_id . "/feed';
var params = 'link=' + mentLink + '&message=[' + mentUsername +'+said:]+' + mentText + '&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://www.testically/2011/09/27/5-steps-to-automatically-write-on-your-facebook-page-wall-using-the-graph-api-without-a-logged-in-user/
http://php-academy.blogspot./2011/04/how-to-post-from-facebook-app-to.html
本文标签: Posting comment to Facebook from JavaScriptStack Overflow
版权声明:本文标题:Posting comment to Facebook from JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744650361a2617647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论