简体   繁体   English

使用php-sdk在Facebook中共享链接

[英]Share link in facebook with php-sdk

I'm trying to share a link on Facebook with the php-sdk. 我正在尝试与php-sdk共享Facebook上的链接。 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 使用https://developers.facebook.com/tools/explorer中的 Graph API Explorer并选择我创建的应用程序和页面的管理员用户,它为我提供了访问令牌并可以完美地共享链接,但是可以通过代码使用相同的令牌,我总是会收到错误

Graph returned an error: Invalid OAuth access token. 图表返回了错误:无效的OAuth访问令牌。

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. 我知道这是身份验证错误,但是,避免身份验证不是使用令牌的目的吗?如果不是,则如何记录管理用户的任务,以及为什么在https://developers.facebook中未提及。 com / docs / php / howto / example_post_links / 5.0.0 。想法是与用户页面管理员(而不是与登录我网站的用户)共享链接。 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. 我想将访问令牌添加到$linkData应该也可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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