简体   繁体   中英

Share link in facebook with php-sdk

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM