2015-04-06 108 views
1

我嘗試了一個示例代碼發佈到Facebook頁面。但它發佈爲我自己。我已經包含Page_access_token。PHP cURL:發佈到Facebook頁面爲頁面

授予的權限是'manage_pages'和'publish_action'。 這裏是我的代碼:

<?php 

    $page_id='xxxx'; 
    $page_access_token='cccc'; 
    $url="https://graph.facebook.com/{$page_id}/feed?message=Hello&access_token=".$page_access_token; 
    $ch=curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_REFERER, ''); 
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); 
    curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 

    $value = json_decode(curl_exec($ch)); 
    $var_dump($value); 

?> 

我在哪裏出錯了?我如何解決它? 我希望它作爲頁面發佈。由於

編輯: 我與/ $ PAGE_ID新的代碼字段=的access_token:

<?php 
    $page_id='xxx'; 
    $message='helloworld'; 
    $url="https://graph.facebook.com/v2.3/{$page_id}?fields=access_token"; 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_REFERER, ''); 
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); 
    curl_setopt($curl, CURLOPT_AUTOREFERER, true); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $message); 
    curl_setopt($curl, CURLOPT_TIMEOUT, 10); 

    $json = json_decode(curl_exec($curl)); 
    var_dump($json); 
?> 

這將返回一個錯誤:

object(stdClass)#1 (1) { 
    ["error"]=> 
    object(stdClass)#2 (3) { 
    ["message"]=> 
    string(64) "(#210) A page access token is required to request this resource." 
    ["type"]=> 
    string(14) "OAuthException" 
    ["code"]=> 
    int(210) 
    } 
} 

我要去哪裏錯了?

我是否需要從圖形API瀏覽器創建page_access_token?或者上面的url足夠了?我該如何使用User_access_token來獲取page_access_token?

+0

你會得到什麼回報? – 2015-04-06 06:18:21

+0

它返回一個ID,這樣的事情 stdClass的對象 ( [ID] => 1387602394895539_1390602811262164 ) 消息被髮布到網頁,但我自己沒有 – SunShine 2015-04-06 06:32:24

回答

1

如果它發佈爲您自己,則表示您沒有使用Page Token。調試令牌,看看頁ID列 - that's你怎麼知道it'sa頁令牌:https://developers.facebook.com/tools/debug/

順便說一句,你應該使用新publish_pages留言的權限頁:https://developers.facebook.com/docs/apps/changelog#v2_3_changes

有關信息如何獲得訪問令牌:

+0

頁我都嘗試「manage_pages」和「publish_pages」太多。如果我只使用那兩個,就沒有任何事情發佈。如何獲得頁面標記?我需要一個用戶令牌來獲得Page Token嗎? – SunShine 2015-04-06 10:22:11

+0

是的。我會添加一些鏈接 – luschn 2015-04-06 10:34:50

+0

這是一個很好的鏈接。但是我怎麼能用cURL來實現呢? – SunShine 2015-04-06 12:00:25

0
<?php 
    $page_id='xxx'; 
    $message='helloworld'; 
$access_token = "XXXXXXXXXXXXXXXXXXXXX"; 
    $url="https://graph.facebook.com/v2.3/{$page_id}/feed/?access_token=".access_token ."&message=".urlencode(message); 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_REFERER, ''); 
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); 
    curl_setopt($curl, CURLOPT_AUTOREFERER, true); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $message); 
    curl_setopt($curl, CURLOPT_TIMEOUT, 10); 

    $json = json_decode(curl_exec($curl)); 
    var_dump($json); 
?> 
+1

歡迎來到Stackoverflow!請注意,[僅限代碼解答](http://meta.stackexchange.com/questions/148272/is-there-any-benefit-to-allowing-code-only-answers-while-blocking-code-只有-疑問句)。你可以通過詳細闡述來改進你的答案。 – helmbert 2015-05-04 18:47:30

相關問題