4

나는 Facebook 응용 프로그램, Facebook 페이지 및 웹 사이트가 있습니다. 다른 사람이 내 웹 사이트에 댓글을 추가하면 아래 코드에서 댓글 텍스트를 검색합니다. 그 후에 내가하고 싶은 것은 내 Facebook 응용 프로그램이 내 Facebook 페이지에 동일한 주석 텍스트를 게시하게하는 것입니다. 여기,JavaScript에서 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 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. 
     }); 
    }); }; 

답변

4

광범위한 검색 후 그것을 찾고있는 사람들을위한 답변입니다 :

이것은 내가 지금까지 내 웹 사이트에있는 자바 스크립트 코드입니다. 코드 안에있는 주석을 읽으면 자세한 정보를 얻을 수 있습니다.

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); 
} 

A (영구) access_token이 작성 :

http://www.testically.org/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.com/2011/04/how-to-post-from-facebook-app-to.html

관련 문제