2014-11-21 62 views
0

我正在使用以下代碼來檢查用戶是否授予在用戶牆上發佈的權限。我不希望最初在登錄時獲得此許可,因爲根據Facebook的說法,這是一種不好的做法,因爲用戶不知道可以使用此權限。因此,當用戶點擊發布到臉書牆時,我想檢查用戶是否已經授予了發佈權限,如果沒有請求權限。我的代碼檢查我擁有哪些權限並將其存儲在數組列表中,這些權限列表中沒有用戶需要的權限。facebook php sdk - 稍後要求其他權限

問題:但是,當我得到的網址,我點擊它,它只是重定向到重定向url而不是詢問權限。

我的代碼如下:?

<?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 
    } 
} 

>

回聲這將返回以下網址

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

您是從哪裏複製該代碼的? publish_stream自從多年以來已被棄用,您應該考慮使用自動加載器而不是所有這些無數的require-statement。 – luschn 2014-11-21 08:36:52

回答

0

你問權限的方式不正確。您應該更改您的代碼這個:

$params = array('email', 'publish_actions'); 
$helper = new FacebookRedirectLoginHelper($weburl); 
$loginUrl = $helper->getLoginUrl($params); 

傳遞給getLoginUrl參數$params是權限的數組。您不需要像舊的PHP SDK那樣添加scope

+0

工作感謝! – 2014-11-21 09:31:54