2012-02-16 47 views
0

我正在使用facebook-php-sdk,我的要求是將故事發布到用戶的牆上。我的應用在註冊/登錄時向用戶請求'publish_stream'權限。在用戶已經允許並登錄之後,以某種方式,我無法將在用戶創建的新評論發佈到Facebook牆上的應用程序中。這是我的錯誤:Facebook-向用戶牆發佈故事

Invalid OAuth access token signature 

這是我做的:

$facebook = new Facebook(array(
           'appId' => "$fb_app_id", 
           'secret' => "$fb_secret", 
           'cookie' => true 
         )); 
try { 
      $fb_user_id = $facebook->getUser(); 
      $access_token = $facebook->getAccessToken(); 
      $facebook->api("/me/feed", 'post', array(
             'access_token' => $access_token, 
             'message' => 'I love SO', 
             'link' => 'http://mydomain.com', 
             'picture' => 'http://thinkdiff.net/ithinkdiff.png', 
             'name' => 'iOS Apps & Games', 
             'description'=> 'Checkout iOS apps and games from iThinkdiff.net. I found some of them are just awesome!' 
            ) 
          ); 
      } catch (FacebookApiException $e) { 
       echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>'; 
      } 

分享您的想法,請

+0

是你在facebook應用程序或外部網站中運行此代碼?在第一種情況下,嘗試刪除api-method-call中的訪問令牌。 – 2012-02-16 10:15:14

+0

您是否使用Facebook PHP SDK的第3版? – fire 2012-02-16 10:15:43

+0

@Blauesocke:我正在外部網站上使用它。我試圖在api方法調用中刪除訪問令牌,但同樣的問題依然存在。 – Ninja 2012-02-16 10:25:39

回答

1

您可以使用下面的代碼發佈到Facebook塗鴉牆,只需調用函數與正確的參數

試試這個,

<?php 


function doWallPost($postName='',$postMessage='',$postLink='',$postCaption='',$postDescription='') 
{ 
$FB_APP_ID='xxxxxxxxxxxxxxxxxxxxxxxx'; 
$FB_APP_SECRET='xxxxxxxxxxxxxxxxxxxxxxxxxxx'; 

$APP_RETURN_URL=((substr($_SERVER['SERVER_PROTOCOL'],0,4)=="HTTP")?"http://":"https://").$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']; 

$code = $_REQUEST["code"]; 

if(empty($code)) 
{ 
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=".$FB_APP_ID."&redirect_uri=".$APP_RETURN_URL."&scope=publish_stream";     
    header("Location:$dialog_url"); 
} 

$token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$FB_APP_ID."&redirect_uri=".urlencode($APP_RETURN_URL)."&client_secret=".$FB_APP_SECRET."&code=".$code; 
$access_token = file_get_contents($token_url); 

$param1=explode("&",$access_token); 
$param2=explode("=",$param1[0]); 
$FB_ACCESS_TOKEN=$param2[1]; 


$url = "https://graph.facebook.com/me/feed"; 
$attachment = array( 'access_token' => $FB_ACCESS_TOKEN,       
       'name'   => $postName, 
       'link'   => $postLink, 
       'description' => $postDescription, 
       'message'  => $postMessage, 
       'caption'  => $postCaption, 
      ); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,2); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment); 
$result=curl_exec($ch); 
header('Content-type:text/html'); 
curl_close($ch); 

return $result 
} 







?> 
+0

不要忘記在上面的代碼中給你的appid和你的祕密 – 2012-02-16 11:12:36

+0

@Ninja做一些upvote,如果這爲你工作 – 2012-02-17 06:50:01