dou9022 2017-08-08 06:51
浏览 51
已采纳

使用API​​将帖子上传到Facebook粉丝页面

I wrote a code to upload videos to my Facebook fanpage, but it gets uploaded to the community of my fanpage.

May someone help me, what is the problem with my code?

My code:

$appId = 'xxxxxxxxxxx';
$appSecret ='xxxxxxxxxx';

$my_url = ':my website url';
$perms_str = "publish_actions";

$videoPath = 'xxxxxxxxxx';
$title = 'xxxxxxxxxx';
$descriptions = 'xxxxxxxxxxx';

if (!isset($_GET['code'])) {
    $auth_url = "http://www.facebook.com/dialog/oauth?client_id="
        . $appId . "&redirect_uri=" . urlencode($my_url)
        . "&scope=" . $perms_str;
    echo("<script>top.location.href='" . $auth_url . "'</script>");
} else {
    $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
        . $appId . "&redirect_uri=" . $my_url
        . "&client_secret=" . $appSecret
        . "&code=" . $_GET['code'];

    $response = file_get_contents($token_url);
    $access_token = Json::decode($response)['access_token'];

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

    $pageId = 'xxxxxxxxxxxx';

    $fileToUpload = $videoPath ;
    $whereUpload = '/'.$pageId.'/videos';

    $data = [
        'title' => $_SESSION['fileToUpload']['title'],
        'caption' => $_SESSION['fileToUpload']['descriptions'],
        'source' => $fb->fileToUpload($fileToUpload)
    ];
}

try {
    $response = $fb->post($whereUpload, $data, $access_token);
} catch (Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}
  • 写回答

1条回答 默认 最新

  • douzhi7754 2017-08-08 10:51
    关注

    thank you cBroe , i added this code and this worked

     $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
        . $appId . "&redirect_uri=" . $my_url
        . "&client_secret=" . $appSecret
        . "&code=" . $_GET['code'];
    
     $response = file_get_contents($token_url);
     $access_token = Json::decode($response)['access_token'];
    
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_URL, 
         'https://graph.facebook.com/v2.10/'.$pageId.'/?
          access_token='.$access_token.'&debug=all&fields=access_token&format=json 
          &method=get&pretty=0&suppress_http_code=1');
      $content = curl_exec($ch);
      $pageAccessToken=Json::decode($content)['access_token'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?