我使用以下代码检查用户是否已授权在用户的墙上发布。我不想在登录时首先获得此权限,因为根据facebook这是一种不好的做法,因为用户不知道这个权限可能用于什么。因此,当用户点击发布到Facebook墙时,我想检查用户是否已经授予了发布权限,如果没有请求权限。我的代码检查我拥有哪些权限,并存储在用户不具备和需要的权限数组列表中。
问题:但是当我收到网址并点击它时,它只是将我重定向到重定向网址而不是请求权限。
我的代码如下:
<?php
$permsNeeded = array();
$permsNeeded[] = "email";
$permsNeeded[] = "publish_stream";
require_once( 'includes/Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'includes/Facebook/HttpClients/FacebookCurl.php' );
require_once( 'includes/Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'includes/Facebook/Entities/AccessToken.php' );
require_once( 'includes/Facebook/Entities/SignedRequest.php' );
require_once( 'includes/Facebook/FacebookSession.php' );
require_once( 'includes/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'includes/Facebook/FacebookRequest.php' );
require_once( 'includes/Facebook/FacebookResponse.php' );
require_once( 'includes/Facebook/FacebookSDKException.php' );
require_once( 'includes/Facebook/FacebookRequestException.php' );
require_once( 'includes/Facebook/FacebookOtherException.php' );
require_once( 'includes/Facebook/FacebookAuthorizationException.php' );
require_once( 'includes/Facebook/GraphObject.php' );
require_once( 'includes/Facebook/GraphSessionInfo.php' );
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
FacebookSession::setDefaultApplication("$appid","$appsecret");
$facebook = new FacebookRedirectLoginHelper("$redirectto");
if(isset($_SESSION['token'])){
$fbsession = new FacebookSession($_SESSION['token']);
try{
$fbsession->Validate($id, $secret);
}catch(FacebookAuthorizationException $e){
$fbsession = '';
}
}
if ( isset( $fbsession ) ) {
$_SESSION['token'] = $fbsession->getToken();
$request = new FacebookRequest( $fbsession, 'GET', '/me/permissions' );
$response = $request->execute();
$graphObject = $response->getGraphObject();
$totalproperties = count($graphObject->getPropertyNames());
$permsArray = array();
for ($i = 0; $i < $totalproperties; $i++) {
$permsArray[] = $graphObject->getProperty($i)->GetProperty('permission');
}
$permsToPrompt = array();
for ($i = 0; $i < count($permsNeeded); $i++) {
if (!in_array($permsNeeded[$i],$permsArray)) {
$permsToPrompt[] = $permsNeeded[$i];
}
}
if (count($permsToPrompt) > 0) {
$scopeparams = implode(",",$permsToPrompt);
$params = array(
'scope' => $scopeparams
);
$helper = new FacebookRedirectLoginHelper($weburl);
$loginUrl = $helper->getLoginUrl($params);
echo $loginUrl;
echo "<a href=\"$loginUrl\">test</a>";
//header("Refresh:0;URL=$reloadurl");
}
else {
//Post
}
}
&GT;
在回显时,返回以下网址
https://www.facebook.com/v2.2/dialog/oauth?client_id={app_id_here}&redirect_uri={redirect_url_is_written_here}&state={some_value_here}&sdk=php-sdk-4.0.12&scope=publish_stream
答案 0 :(得分:1)
您要求权限的方式不正确。您应该将代码更改为:
$params = array( 'email', 'publish_actions' );
$helper = new FacebookRedirectLoginHelper( $weburl );
$loginUrl = $helper->getLoginUrl( $params );
传递给$params
的参数getLoginUrl
是一组权限。您不需要像旧的PHP SDK一样添加scope
。