2012-06-28 91 views
0

我用下面的代碼來獲取的access_token的Facebook SDK 3.1.1 ACCESS_TOKEN問題

<?php 
//include the Facebook PHP SDK 
include_once 'facebook.php'; 

//instantiate the Facebook library with the APP ID and APP SECRET 
$facebook = new Facebook(array(
    'appId' => 'REPLACE WITH APP ID', 
    'secret' => 'REPLACE WITH APP SECRET', 
    'cookie' => true 
)); 

//Get the FB UID of the currently logged in user 
$user = $facebook->getUser(); 

//if the user has already allowed the application, you'll be able to get his/her FB UID 
if($user) { 
    //start the session if needed 
    if(session_id()) { 

    } else { 
     session_start(); 
    } 

    //do stuff when already logged in 

    //get the user's access token 
    $access_token = $facebook->getAccessToken(); 
    //check permissions list 
    $permissions_list = $facebook->api(
     '/me/permissions', 
     'GET', 
     array(
      'access_token' => $access_token 
     ) 
    ); 

    //check if the permissions we need have been allowed by the user 
    //if not then redirect them again to facebook's permissions page 
    $permissions_needed = array('publish_stream', 'read_stream', 'offline_access', 'manage_pages'); 
    foreach($permissions_needed as $perm) { 
     if(!isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1) { 
      $login_url_params = array(
       'scope' => 'publish_stream,read_stream,offline_access,manage_pages', 
       'fbconnect' => 1, 
       'display' => "page", 
       'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] 
      ); 
      $login_url = $facebook->getLoginUrl($login_url_params); 
      header("Location: {$login_url}"); 
      exit(); 
     } 
    } 

    //if the user has allowed all the permissions we need, 
    //get the information about the pages that he or she managers 
    $accounts = $facebook->api(
     '/me/accounts', 
     'GET', 
     array(
      'access_token' => $access_token 
     ) 
    ); 

    //save the information inside the session 
    $_SESSION['access_token'] = $access_token; 
    $_SESSION['accounts'] = $accounts['data']; 
    //save the first page as the default active page 
    $_SESSION['active'] = $accounts['data'][0]; 

    //redirect to manage.php 
    header('Location: manage.php'); 
} else { 
    //if not, let's redirect to the ALLOW page so we can get access 
    //Create a login URL using the Facebook library's getLoginUrl() method 
    $login_url_params = array(
     'scope' => 'publish_stream,read_stream,offline_access,manage_pages', 
     'fbconnect' => 1, 
     'display' => "page", 
     'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] 
    ); 
    $login_url = $facebook->getLoginUrl($login_url_params); 

    //redirect to the login URL on facebook 
    header("Location: {$login_url}"); 
    exit(); 
} 

?> 

但它不是又回到了我的重定向URL,火狐顯示以下錯誤

Firefox has detected that the server is redirecting the request for this address in a way that will never complete. 


    This problem can sometimes be caused by disabling or refusing to accept 
    cookies. 
+0

不要忘了'offline_access'許可,現在已經過時,請參見:http://developers.facebook.com/roadmap/offline-access-removal/ –

回答

1

你可以把登錄和身份驗證過程中像的login.php 然後當你需要進行身份驗證,然後使用從您的應用程序include_once "login.php";另一個文件。 替換您的應用程序名稱空間。

<?php 
//login.php 
require 'lib/facebook.php'; 
require 'lib/fbconfig.php'; 





if (isset($_GET['code'])){ 
     header("Location:http://apps.facebook.com/your-app-namespace"); 
     exit; 
    } 
$user=null; 




//Facebook Authentication part 
    $user  = $facebook->getUser(); 
    // We may or may not have this data based 
    // on whether the user is logged in. 
    // If we have a $user id here, it means we know 
    // the user is logged into 
    // Facebook, but we don’t know if the access token is valid. An access 
    // token is invalid if the user logged out of Facebook. 

    $loginUrl = $facebook->getLoginUrl(
      array(
       'scope'   => 'email,read_mailbox,publish_stream,user_birthday,user_location,read_stream,user_work_history,user_about_me,user_hometown' 
      ) 
    ); 

    if ($user) { 
     try { 
     // Proceed knowing you have a logged in user who's authenticated. 
     $user_profile = $facebook->api('/me'); 
     } catch (FacebookApiException $e) { 
     //you should use error_log($e); instead of printing the info on browser 
     d($e); // d is a debug function defined at the end of this file 
     $user = null; 
     } 
    } 

    if (!$user) { 
     echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>"; 
     exit; 
    } 

    //get user basic description 
    $userInfo   = $facebook->api("/$user"); 

    function d($d){ 
     echo '<pre>'; 
     print_r($d); 
     echo '</pre>'; 
    } 




?> 
1

012:除了使用使用

//redirect to the login URL on facebook 
header("Location: {$login_url}"); 

嘗試

echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>"; 
+0

它只是開始清爽頁面汽車無 – Asghar

+0

檢查我的其他答案。 – ASHUTOSH