Post 在 Facebook profile/page 时间线上使用 Facebook Graph API v3.1 PHP SDK
Post on Facebook profile/page timeline using Facebook Graph API v3.1 PHP SDK
如您所知,Facebook 于 2018 年 7 月 26 日推出了新的 Graph API v3.1,这为开发人员带来了很多改变。我现在的问题之一是如何使用 Facebook Graph API v3.1 PHP SDK share/post 在 Facebook profile/page 时间线上?
Reminder: Graph API v2.7 will be deprecated on Oct 05, 2018. Please use the API Upgrade Tool to understand how this might impact your app. For more details see the changelog
为此,我创建了一个新应用程序,其中包含一些设置,如下面的屏幕截图所示。
为此,我使用了下面提到的代码以及 facebook-php-graph-sdk-5.x。
fbConfig.php
<?php
if(!session_id()){
session_start();
}
// Include the autoloader provided in the SDK
require_once __DIR__ . '/facebook-php-graph-sdk-5.x/autoload.php';
// Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
/*
* Configuration and setup Facebook SDK
*/
$appId = 'APP_ID'; //Facebook App ID
$appSecret = 'APP_SECRET'; //Facebook App Secret
$redirectURL = 'MAIN_PAGE_URL_SAME_AS_IN_APPS_SETTING'; //Callback URL
$fbPermissions = array('publish_actions'); //Facebook permission
$fb = new Facebook(array(
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.6',
));
// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();
// Try to get access token
try {
if(isset($_SESSION['facebook_access_token'])){
$accessToken = $_SESSION['facebook_access_token'];
}else{
$accessToken = $helper->getAccessToken();
}
} catch(FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
?>
index.php
<?php
// Include FB configuration file
require_once 'fbConfig.php';
if(isset($accessToken)){
if(isset($_SESSION['facebook_access_token'])){
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}else{
// Put short-lived access token in session
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler helps to manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// Set default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
//FB post content
$message = 'Test message from whosebug.com website';
$title = 'Post From Website';
$link = 'http://www.whosebug.com/';
$description = 'Whosebug is simply awesome.';
$picture = 'https://i.stack.imgur.com/MybMA.png';
$attachment = array(
'message' => $message,
'name' => $title,
'link' => $link,
'description' => $description,
'picture'=>$picture,
);
try{
// Post to Facebook
$fb->post('/me/feed', $attachment, $accessToken);
// Display post submission status
echo 'The post was published successfully to the Facebook timeline.';
}catch(FacebookResponseException $e){
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}catch(FacebookSDKException $e){
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}else{
// Get Facebook login URL
$fbLoginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);
// Redirect to Facebook login page
echo '<a href="'.$fbLoginURL.'"><img src="https://www.freeiconspng.com/uploads/facebook-login-button-png-11.png" /></a>';
}
?>
我的文件等级如下图所示。
最后,在完成上述所有设置和代码后,当我尝试 运行 我的页面时,我得到了 LOGIN FACEBOOK BUTTON,单击那里后,我得到了以下屏幕截图错误。
我想要的很简单post我想要的内容,无需显示任何 POPUP 或对话框,这样我就可以通过我的 PHP 代码轻松使用它。
我试图在互联网上找到任何可行的解决方案,但现在都已经过时了,现在没有任何东西可以使用新的 Facebook Graph API v3.1。
What I want is simple post my desired content without showing any POPUP or dialog
用户个人资料不再可能。您必须改为使用共享对话框。 publish_actions
已删除,没有替代品或解决方法。
对于页面,您可以使用 manage_pages
和 publish_pages
到 post 到带有页面令牌的页面。
是的,您应该 post 在页面提要上而不是个人提要上。
正如 luschn 所说,您需要 manage_pages
和 publish_pages
权限才能获取正确页面的访问令牌。
首先,您必须在开发模式下开发您的应用,然后您需要使用这些权限提交应用审核。
此过程需要您上传带演示的截屏视频并将应用放在 public 位置,以便 Facebook 审核团队可以访问并测试它。
任重而道远..
如您所知,Facebook 于 2018 年 7 月 26 日推出了新的 Graph API v3.1,这为开发人员带来了很多改变。我现在的问题之一是如何使用 Facebook Graph API v3.1 PHP SDK share/post 在 Facebook profile/page 时间线上?
Reminder: Graph API v2.7 will be deprecated on Oct 05, 2018. Please use the API Upgrade Tool to understand how this might impact your app. For more details see the changelog
为此,我创建了一个新应用程序,其中包含一些设置,如下面的屏幕截图所示。
为此,我使用了下面提到的代码以及 facebook-php-graph-sdk-5.x。
fbConfig.php
<?php
if(!session_id()){
session_start();
}
// Include the autoloader provided in the SDK
require_once __DIR__ . '/facebook-php-graph-sdk-5.x/autoload.php';
// Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
/*
* Configuration and setup Facebook SDK
*/
$appId = 'APP_ID'; //Facebook App ID
$appSecret = 'APP_SECRET'; //Facebook App Secret
$redirectURL = 'MAIN_PAGE_URL_SAME_AS_IN_APPS_SETTING'; //Callback URL
$fbPermissions = array('publish_actions'); //Facebook permission
$fb = new Facebook(array(
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.6',
));
// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();
// Try to get access token
try {
if(isset($_SESSION['facebook_access_token'])){
$accessToken = $_SESSION['facebook_access_token'];
}else{
$accessToken = $helper->getAccessToken();
}
} catch(FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
?>
index.php
<?php
// Include FB configuration file
require_once 'fbConfig.php';
if(isset($accessToken)){
if(isset($_SESSION['facebook_access_token'])){
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}else{
// Put short-lived access token in session
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler helps to manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// Set default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
//FB post content
$message = 'Test message from whosebug.com website';
$title = 'Post From Website';
$link = 'http://www.whosebug.com/';
$description = 'Whosebug is simply awesome.';
$picture = 'https://i.stack.imgur.com/MybMA.png';
$attachment = array(
'message' => $message,
'name' => $title,
'link' => $link,
'description' => $description,
'picture'=>$picture,
);
try{
// Post to Facebook
$fb->post('/me/feed', $attachment, $accessToken);
// Display post submission status
echo 'The post was published successfully to the Facebook timeline.';
}catch(FacebookResponseException $e){
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}catch(FacebookSDKException $e){
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}else{
// Get Facebook login URL
$fbLoginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);
// Redirect to Facebook login page
echo '<a href="'.$fbLoginURL.'"><img src="https://www.freeiconspng.com/uploads/facebook-login-button-png-11.png" /></a>';
}
?>
我的文件等级如下图所示。
最后,在完成上述所有设置和代码后,当我尝试 运行 我的页面时,我得到了 LOGIN FACEBOOK BUTTON,单击那里后,我得到了以下屏幕截图错误。
我想要的很简单post我想要的内容,无需显示任何 POPUP 或对话框,这样我就可以通过我的 PHP 代码轻松使用它。
我试图在互联网上找到任何可行的解决方案,但现在都已经过时了,现在没有任何东西可以使用新的 Facebook Graph API v3.1。
What I want is simple post my desired content without showing any POPUP or dialog
用户个人资料不再可能。您必须改为使用共享对话框。 publish_actions
已删除,没有替代品或解决方法。
对于页面,您可以使用 manage_pages
和 publish_pages
到 post 到带有页面令牌的页面。
是的,您应该 post 在页面提要上而不是个人提要上。
正如 luschn 所说,您需要 manage_pages
和 publish_pages
权限才能获取正确页面的访问令牌。
首先,您必须在开发模式下开发您的应用,然后您需要使用这些权限提交应用审核。
此过程需要您上传带演示的截屏视频并将应用放在 public 位置,以便 Facebook 审核团队可以访问并测试它。
任重而道远..