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.

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

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧