2011-04-07 59 views
0

我需要張貼到我自己的牆壁前進行身份驗證,所以這裏是我的代碼認證Facebook應用程序 - PHP

function get_app_token($appid, $appsecret) 
{ 
$args = array(
'grant_type' => 'client_credentials', 
'client_id' => $appid, 
'client_secret' => $appsecret 
); 

$ch = curl_init(); 
$url = 'https://graph.facebook.com/oauth/access_token'; 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $args); 
$data = curl_exec($ch); 

return json_encode($data); 
} 

$token = get_app_token($appid, $appsecret); 
$attachment = array('message' => '', 
     'access_token' => $token, 
       'name' => 'Attachment Name', 
       'caption' => 'Attachment Caption', 
       'link' => 'http://apps.facebook.com/xxxxxx/', 
       'description' => 'Description .....', 
       'picture' => 'http://www.google.com/logo.jpg', 
       'actions' => array(array('name' => 'Action Text', 
            'link' => 'http://apps.facebook.com/xxxxxx/')) 
       ); 

$result = $facebook->api('/me/feed/', 'post', $attachment); 

我得到錯誤OAuthException ,無效的OAuth訪問令牌簽名。

+0

的[需要對張貼在牆上用戶幫助]可能重複(http://stackoverflow.com/questions/5126665/need-幫助發佈在用戶牆上) – ifaour 2011-04-08 11:20:58

+0

我上面發佈的鏈接將幫助您入門。您正在申請一個「應用程序訪問令牌」!這不是用來代表用戶完成任務的! – ifaour 2011-04-08 11:21:53

回答

0

您可以直接從facebook對象的會話數組中獲取access_token。

$session = $facebook->getSession(); 
$session['access_token']; 

當然你需要被授權。如果沒有設置會話,您必須重定向到loginUrl

$facebook->getLoginUrl(array('req_perms' => 'email,read_stream,publish_stream')); 
// this will return url for your authorization 

和您需要的請求權限。

+0

如果我告訴你所有的代碼你可以修復它嗎? – Sourav 2011-04-07 13:18:26

-1

A client_credentials訪問令牌不給任何訪問/me(誰會「我」在這種情況下?沒有任何關係到用戶),更不用說張貼訪問權限。它僅用於應用程序級圖形API調用,例如獲取應用程序的Insights數據。

+0

你能幫我解決這個問題嗎? – Sourav 2011-04-07 14:05:21

+0

你需要完全重寫你的oauth流。 – ceejayoz 2011-04-07 14:14:00

+0

我很新的FB應用程序,我今天剛剛開始,所以我需要代碼:| – Sourav 2011-04-07 14:18:32

0

$token價值爲access_token=<YOUR_ACCESS_TOKEN>

你不能直接傳遞$token價值的access_token參數。

$token = get_app_token($appid, $appsecret); 
$access_token = split('=',$token); 
$attachment = array(
        'message' => '', 
        'access_token' => $access_token[1], 
        'name' => 'Attachment Name', 
        'caption' => 'Attachment Caption', 
        'link' => 'http://apps.facebook.com/xxxxxx/', 
        'description' => 'Description .....', 
        'picture' => 'http://www.google.com/logo.jpg', 
        'actions' => array(array('name' => 'Action Text', 
           'link' => 'http://apps.facebook.com/xxxxxx/')) 
      ); 

希望這將有助於使這項工作... :)

+0

我很感謝你的幫助,但它返回了同樣的錯誤,請看看我的代碼http://www.web-shine.in/tutorials/fb/code.txt – Sourav 2011-04-08 03:02:14

+0

'$ token'的值是多少? – 2011-04-08 05:52:00