使用 API 将 post 上传到 Facebook 粉丝专页
Upload post to Facebook fanpage with API
我写了一个代码来将视频上传到我的 Facebook 粉丝专页,但它被上传到我的粉丝专页的社区。
有人可以帮助我吗,我的代码有什么问题?
我的代码:
$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;
}
谢谢 cBroe,我添加了这段代码并且成功了
$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'];
我写了一个代码来将视频上传到我的 Facebook 粉丝专页,但它被上传到我的粉丝专页的社区。
有人可以帮助我吗,我的代码有什么问题?
我的代码:
$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;
}
谢谢 cBroe,我添加了这段代码并且成功了
$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'];