Facebook OAuthError - 无法将公开帖子发布到用户的Feed

时间:2013-05-22 13:06:57

标签: facebook error-handling permissions facebook-oauth

我希望能够发布到用户墙(他编辑的消息)。但如果用户不允许代表他发帖,那么我会收到以下错误:

  

(#1)创建共享时发生错误| OAuthException

然后我读到我可以再次通过将他发送到我的登录URL再次请求许可,但即使在允许它之后它也会发送相同的错误。

代码:

try{...
     $ret_obj = $facebook -> api('/' . $user_id . '/feed', 'POST', array('name' => $title, 'link' => $redirect_url, 'caption' => $ptnr_fb_caption, 'icon' => 'http://...logo-small.png', 'picture' => $ptnr_fb_img, 'message' => $desc, 'privacy' => $arrPriv));

} catch(FacebookApiException $e) {

        echo '<div id="text">Error :</div><br /><p style="width: 365px;margin: 0 auto;">Problem authenticating via Facebook, please allow us to share on your behalf.</p>';
        echo '<center><a href="https://www.facebook.com/dialog/oauth?client_id=<my_id>&redirect_uri=http://<mysite>/getFacebookData.php&display=popup&scope=email,publish_stream&type=web_server">Allow here</a><center>';

        //Send email to admin
        $subject = "ERROR Facebook";
        $body = "user email:" . $user_email . '| error:' . $e -> getMessage().'|'.$e->getType();
}

修改 我发现当我再次允许权限时(在收到错误消息后通过链接),只有“everyone”/ public生成错误,而只有我和朋友没有。

更新(@Axel Amthor):

$facebook = new Facebook( array('appId' => 'app_id', 'secret' => 'removed', ));

//We got after the authentication request
$access_token = $_POST['access_token'];

$facebook -> setAccessToken($access_token);
try {
    $user_info = $facebook -> api('/me');
    $user_id = $user_info['id'];
} catch (FacebookApiException $e) {
    //return 0;
}

更新2: 范围相关代码:

$red_url = 'https://www.facebook.com/dialog/oauth?client_id=my_id&redirect_uri=http://mysite/facebook/getFacebookData.php&display=popup&scope=email,publish_stream&type=web_server';

重定向到getFacebookData.php:

$access_token = '';

    $token_url = "https://graph.facebook.com/oauth/access_token?client_id=my_id&redirect_uri=http://mysite/facebook/getFacebookData.php&client_secret=secret&code=" . $_GET['code'];
    $access_token = file_get_contents($token_url);
    $access_token = substr($access_token, 13, strlen($access_token) - (13 + 16));

它将访问令牌发送到tird页面,(更新中的代码:“Update(@Axel Amthor):”)。

2 个答案:

答案 0 :(得分:0)

缺少范围参数(来自我的代码):

$OAuth_request = _formatOAuthReq($OAuth, "read_insights,ads_management,publish_actions,manage_pages,publish_stream,read_stream,offline_access", $state);

....

function _formatOAuthReq($OAuthParams, $scope, $state)
{
    $uri = $OAuthParams['oauth_uri'];
    $uri .= "?client_id=" . $OAuthParams['client_id'];
    $uri .= "&redirect_uri=" . $OAuthParams['redirect_uri'];
    $uri .= "&scope=" . $scope;
    $uri .= "&response_type=code";
    $uri .= "&approval_prompt=force&access_type=offline";

要在用户个人资料中发布内容,请考虑以下权限:

  1. create_event
  2. create_note
  3. photo_upload
  4. publish_stream
  5. read_stream
  6. status_update
  7. video_upload
  8. 电子邮件
  9. publish_actions
  10. 根据/[acctid]/feed,您将在[acctid]标识的用户墙上发布“注释”。实际上,这是使用OAuth对话框中给出的访问令牌“登录”的用户。我刚刚在这里使用了我们的代码,我们正在使用

    /me/feed

    这就像一个魅力。实际上,不是你发布给“外国用户”的人,而是用户自己,通过授予你的应用程序代表他/她的权限,让你的应用程序发布到他/她自己的墙上。方法很复杂,我知道。

答案 1 :(得分:0)

  

我发现当我再次允许权限时(在收到错误消息后通过链接),只有“everyone”/ public产生错误,而只有我和朋友不会。

所以你想强制用户公开发布?你不应该这样做 - 让用户决定!

也许在您的应用设置中将“public”设置为默认操作隐私 - 然后用户可以根据需要将其更改为更具限制性的值,可以直接从登录对话框或稍后在他们的个人资料应用设置。

然后在API调用中根本不提供privacy参数,以便自动应用用户选择的值。

相关问题