2013-02-01 44 views
1

關於臉譜圖api的一個小問題。 我一直在使用官方Facebook Graph API PHP SDK發表評論到網頁上,使用臉譜圖api

我還在我的網頁上添加了facebook社交評論插件小部件。 使用Facebook的Graph api可以發表評論到網頁嗎?

用戶將通過Facebook登錄到我的Web應用程序,然後他必須能夠通過API在我的應用程序的網頁上發佈Facebook評論。所以小部件將顯示所有直接在頁面上發佈的評論以及通過API發佈的評論。

到目前爲止,我已經試過這AJAX代碼:

<?php 
$token = $facebook->getAccessToken(); 
?> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" 
type="text/javascript"></script> 
<script type="text/javascript"> 
$.post('https://graph.facebook.com/comments/?ids=http://mywebsite.com/comments.php?id=123',{ name: 'John', message: 'Testing comments', 'method': 'POST', 'format': 'json', 'access_token': '<?php echo $token; ?>'}, 
function(data) {   
}); 
</script> 

但這返回一個OAuth例外:

{ 
    "error": { 
     "message": "An unknown error has occurred.", 
     "type": "OAuthException", 
     "code": 1 
    } 
} 

我請求足夠的權限在設置登錄網址:

$loginUrl = $facebook->getLoginUrl(array(
       'scope' => 'email,publish_stream,publish_actions,read_stream,user_about_me,user_events,create_event' 
      )); 

說明: 我包括我的頁面上的Facebook構件,使用下面的代碼:

<div class="fb-comments" data-href="http://www.mywebsite.com/comments.php? 
id=<?php echo $_GET['id']; ?>" data-width="370" data-num-posts="5"></div> 

腳本:

<div id="fb-root"></div> 
<script>(function(d, s, id) { 
    var js, fjs = d.getElementsByTagName(s)[0]; 
    if (d.getElementById(id)) return; 
    js = d.createElement(s); js.id = id; 
    js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=xxxxxxxxxxxxxxx"; 
    fjs.parentNode.insertBefore(js, fjs); 
}(document, 'script', 'facebook-jssdk'));</script> 


    <div id='fb-root'></div> 
    <script src='http://connect.facebook.net/en_US/all.js'></script> 
    <!--<p><a onclick='postToFeed(); return false;'>Post to Feed</a></p>--> 
    <p id='msg'></p> 

    <script> 
     FB.init({appId: "xxxxxxxxxxxxxxx", status: true, cookie: true}); 

     function postToFeed() { 

     // calling the API ... 
     var obj = { 
      method: 'feed', 
      redirect_uri: 'http://www.mywebsite.com/comments.php?id=<?php echo $_GET['id']; ?>', 
      link: 'https://developers.facebook.com/docs/reference/dialogs/', 
      picture: 'http://fbrell.com/f8.jpg', 
      name: 'Facebook Dialogs', 
      caption: 'Reference Documentation', 
      description: 'Using Dialogs to interact with users.' 
     }; 

     function callback(response) { 
      document.getElementById('msg').innerHTML = "Post ID: " + response['post_id']; 
     } 
     FB.ui(obj, callback); 
     }  
    </script> 

回答

1

這不是張貼評論正確的語法。至少在帖子ID丟失: 在這裏,您如何通過API調用它(PHP SDK)

$facebook ->api('/'.$post_id.'/comments', 
'post', 
    array(
     'message' => 'users comment', 
    ) 
); 

不能使用JQuery的$。員額用於生成API調用。使用JQuery通過API調用來調用PHP腳本。

+0

我不知道用於post_id的是什麼。它是我的網頁的網址,有Facebook的評論部件? –

+0

postId =評論所屬帖子的ID。如果沒有API,不知道在哪裏發表評論。來自Facebook的部件?它是基於iFrame還是Javascript?你自己寫了嗎? –

+0

該小部件不是從Facebook。我已使用iframe代碼手動將小部件包含在我的頁面中。 –