【问题标题】:Posting to facebook page wall via curl通过 curl 发布到 facebook 页面墙
【发布时间】:2012-02-23 16:26:29
【问题描述】:

我正在尝试使用 curl 将帖子发布到我的 facebook 页面墙上,但我不断收到以下错误

The user hasn't authorized the application to perform this action

如何使用 curl 生成正确的访问令牌?如果你去这个网址http://developers.facebook.com/tools/explorer/

然后我可以输入以下https://graph.facebook.com/337588736279406/feed,这将显示我的墙提要,然后我可以将其更改为发布并为消息内容添加一个字段,如果我运行它,我会得到以下响应。

    {
  "error": {
    "message": "(#200) The user hasn't authorised the application to perform this action", 
    "type": "OAuthException", 
    "code": 200
  }
}

我需要做什么才能授权我成为用户?

有人可以帮我解决这个问题吗?

    <?php

function postify($arr) {
    $fields_string = '';
    foreach ($arr as $key => $value) {
        $fields_string .= $key . '=' . $value . '&';
    }
    return rtrim($fields_string, '&');
}


$appid = 'app id';
$redirect_url = 'http://stickynote.users36.interdns.co.uk';
$app_secret = 'app secret';
$page_id = '337588736279406';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id='.$appid.'&redirect_uri='.$redirect_url.'&client_secret='.$app_secret.'&perms=publish_stream');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);

$output = explode("=", $output);

curl_close($ch);

$postdata = array(
    'message' => 'this is a test message',
);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/'.$page_id.'/feed?access_token='.$output[1].'');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, postify($postdata));
$check = curl_exec($ch);

print_r($check);        

?>

【问题讨论】:

    标签: php facebook facebook-wall


    【解决方案1】:

    为了能够发布内容,您必须向您的应用授予 publish_stream 权限。

    可以使用OAuth Dialog 来请求权限。

    顺便说一句,如果您只是需要它供用户进行测试,您可以通过选择您的应用程序,单击获取访问令牌并检查所需的权限来使用资源管理器工具。

    更新:
    您将无法代表应用程序、仅用户或具有适当access_token 的页面发布(对于页面,它将需要来自accountsuser 连接的manage_pagesaccess_token)。 无需在 access_token 检索 URL 中指定 perms。应该在该步骤之前授予权限。

    【讨论】:

    • 我该怎么做才能帮到你
    • 嗨多汁感谢您的回复我有点困惑我如何通过 curl 返回访问令牌我可以运行您所说的facebook.com/dialog/oauth/… 然后这将返回我与 url 中的代码我该怎么做然后通过 curl 访问它
    • 由于需要在浏览器中进行用户交互,您将无法仅使用 curl 完成所有这些操作...
    • 啊,好吧,所以他们无法通过 curl 自动发布到页面提要
    • 只有在您之前授权用户并授予权限的情况下;)
    【解决方案2】:

    您需要获取页面访问令牌才能执行此操作。

    documentation 中写道:

    页面访问令牌

    用于管理页面的 access_token。这是使用 当您想要执行作为 Page 的操作时。这种访问 通过向 /USER_ID/accounts 发出 HTTP GET 或向 /PAGE_ID?fields=access_token 具有 manage_pages 权限。得到 /USER_ID/accounts 将返回页面列表(包括应用配置文件 页面),除了 每个页面的 access_token。或者,您可以获得页面访问权限 通过发出 HTTP GET 到单个特定页面的令牌 /PAGE_ID?fields=access_token 具有 manage_pages 权限(您 必须通过 fields= 专门询问 access_token 字段 范围)。有关更多信息,请参阅 Page 对象的文档 信息

    获得访问令牌后,您可以进行这些 api 调用。

    【讨论】:

    • 1.如果您想作为页面本身发布,您只需要这个,不需要以用户身份发布。 2. 您需要请求publish_stream 才能发布内容。
    猜你喜欢
    • 2012-11-08
    • 2012-09-13
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 2011-07-14
    • 1970-01-01
    • 2010-10-31
    相关资源
    最近更新 更多