我已经创建了一个简单的画布应用程序。我已经在facebook中设置了我的访问令牌,但这个访问令牌设置了范围public_profile, basic_info, user_friends
我还想添加public_action,以便我也可以测试张贴。
我使用php FacebookSdk 4.0与以下代码
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('{app_id}','{app_secret}');
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch ('Exception $ex) {
echo $ex->getMessage();
}
if($session)
{
$request = new FacebookRequest( $session, 'GET', '/me' );
try{
$response = $request->execute();
$graphObject = $response->getGraphObject();
//echo print_r( $graphObject, 1 );
} catch(FacebookRequestException $ex) {
echo $ex->getMessage();
} catch ('Exception $ex) {
echo $ex->getMessage();
}
try{
$response = (new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => 'www.example.com',
'message' => 'User provided message'
)
))->execute()->getGraphObject();
} catch(FacebookPermissionException $ex) {
echo $ex->getMessage();
} catch ('Exception $ex) {
echo $ex->getMessage();
}
}
else
{
// show login url
}
与误差(#200) The user hasn't authorized the application to perform this action
如何给自己发布权限?
我已经设法使用以下代码获得发布权限
$url = 'https://www.facebook.com/dialog/oauth?client_id=xxx
&redirect_uri=https://xxxx/v1/&scope=publish_actions';
以下是我的代码从Fb开发人员文档
try {
$response = (new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => 'www.example.com',
'message' => 'User provided message'
)
))->execute()->getGraphObject();
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
我得到错误
Exception occured, code: 100 with message: Invalid parameter
您显然可以在getLoginUrl()
$loginUrl = $helper->getLoginUrl(["public_profile", "publish_actions", ...]);
看- https://github.com/facebook/facebook-php-sdk-v4/blob/master/src/Facebook/FacebookRedirectLoginHelper.php别
- https://developers.facebook.com/docs/php/gettingstarted/4.0.0验证
请注意,在v2.0中,您需要在使用应用程序请求权限之前检查您的权限:https://developers.facebook.com/docs/apps/changelog#v2_0_permissions