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
Add a ment  | 

1 Answer 1

Reset to default 4

After 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