0

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

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

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

代码:

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();
}

编辑: 我发现当我再次允许权限时(通过收到错误消息后的链接),只有“所有人”/公众会产生错误,而只有我和朋友不会。

更新(@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));

It sends that access token to the tird page, (code in update: "Update (@Axel Amthor):").

4

2 回答 2

0

Missing scope Parameter like (from my code):

$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";

In order to publish something on the users profile, consider these permissions:

  1. create_event
  2. create_note
  3. photo_upload
  4. publish_stream
  5. read_stream
  6. status_update
  7. video_upload
  8. email
  9. publish_actions

As per /[acctid]/feed you are going to publish a "note" on the users wall identified by [acctid]. Actually, this is the user which has "logged in" with the access token given in the OAuth dialog. I've just poked in to our code over here and we're doing that with

/me/feed

and this is working like a charm. Actually it's not you who is posting to a "foreign user" but it's the user himself, taking your app to post to his/her own wall by granting your app the permission to do so on his/her behalf. Way complicated, I know.

于 2013-05-29T23:09:07.857 回答
0

I found that when I allow the permissions again (via the link after getting the error message), only the "everyone"/ public is generating the error, while only-me and friends do not.

So you want to force the user to post publicly? You shouldn't do that - let the user decide!

Maybe set "public" as the default action privacy in your app settings - then the user can change that to a more restrictive value if they like, either directly from the login dialog or later in their profile app settings.

And then don't give a privacy parameter at all in your API call, so that the value the user has chosen will be applied automatically.

于 2013-05-30T11:04:55.060 回答