PHP cURL : Post to Facebook Page as Page
我尝试将示例代码发布到 Facebook 页面。但它以我自己的身份发布。我已包含 Page_access_token.
授予的权限是 \\'manage_pages\\' 和 \\'publish_action\\'。
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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?fields=access_token:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?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); ?> |
这会返回一个错误:
1 2 3 4 5 6 7 8 9 10 11 | 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 吗?还是上面的网址就足够了?我应该如何使用 User_access_token 来获取 page_access_token ?
如果它以您自己的身份发布,则您没有使用页面令牌。调试您的 Token 并查看是否列出了 Page ID - 那?您是怎么知道它的?是 Page Token:https://developers.facebook.com/tools/debug/
顺便说一句,您应该使用新的
有关如何获取访问令牌的信息:
- https://developers.facebook.com/docs/facebook-login/access-tokens
- http://www.devils-heaven.com/facebook-access-tokens/
- http://www.devils-heaven.com/extended-page-access-tokens-curl/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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); ?> |