dthtvk3666 2015-08-20 16:34
浏览 72
已采纳

用php-sdk分享facebook中的链接

I'm trying to share a link on Facebook with the php-sdk. Using Graph API Explorer in https://developers.facebook.com/tools/explorer and choosing the app that I create and the admin user for the page, it give me the access token and can share the link perfectly, but from the code with the same token I always get the error

Graph returned an error: Invalid OAuth access token.

I understand that is an authentication error but, avoid the authentication is not the purpose of use the token?, if not, how can be logged the admin user for do the task and why is not mentioned in https://developers.facebook.com/docs/php/howto/example_post_links/5.0.0 .The idea is to share the link with the user page administrator, not with the user logged on my site. This is the code.

public function shareOnFacebook($link)
{
    $pageId = "XXXXXXXXXXX";
    $accessToken = "YYYYYYYYYYYYYY";
    $appId = "ZZZZZZZZZZZ";
    $appSecret = "SSSSSSSSSSSSS";

    $fb = new Facebook([
        'app_id' => '{'.$appId.'}',
        'app_secret' => '{'.$appSecret.'}',
        'default_graph_version' => 'v2.2',
    ]);

    $linkData = [
        'link' => 'http://www.example.com',
        'message' => 'User provided message',
    ];

    try {

        $response = $fb->post('/'.$pageId.'/feed', $linkData, '{'.$accessToken.'}');
    } catch(FacebookResponseException $e) {
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch(FacebookSDKException $e) {
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
}
  • 写回答

1条回答 默认 最新

  • dongpu1908 2015-09-09 11:36
    关注

    You should replace the whole field with your access token:

    $response = $fb->post('/'.$pageId.'/feed', $linkData, '{'.$accessToken.'}');
    

    Should be

    $response = $fb->post('/'.$pageId.'/feed', $linkData, $accessToken);
    

    I suppose adding the access token to the $linkData should work as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?