2014-05-20 107 views
0

我試圖發佈爲Facebook頁面上的頁面,因爲6天沒有成功,這真的令人沮喪,使用新的Facebook SDK 4.0.4。PHP Facebook SDK 4.0作爲頁面服務器發佈到服務器

我在PHP 5.4以下(按照Facebook 4.0 SDK版本的要求)。 我已經閱讀了一些所有的facebook文檔(不太清楚)https://developers.facebook.com/docs/reference/php/4.0.0

對於測井在:

<?php 

require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookSession.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRedirectLoginHelper.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequest.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookResponse.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookSDKException.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequestException.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookOtherException.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookAuthorizationException.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/GraphObject.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/GraphSessionInfo.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookServerException.php'); 

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; 
use Facebook\FacebookServerException; 



     session_start(); 


     // init app with app id and secret 
     FacebookSession::setDefaultApplication($site["facebook_appid"],$site["facebook_appsecret"]); 

     // login helper with redirect_uri 
     $helper = new FacebookRedirectLoginHelper($site["url"].$_SERVER['REQUEST_URI']); 

     // see if a existing session exists 
     if (isset($_SESSION) && isset($_SESSION['fb_token'])) { 
      // create new session from saved access_token 
      $session = new FacebookSession($_SESSION['fb_token']); 

      // validate the access_token to make sure it's still valid 
      try { 
      if (!$session->validate()) { 
       $session = null; 
      } 
      } catch (Exception $e) { 
      // catch any exceptions 
      $session = null; 
      } 
      echo '<a href="' . $helper->getLogoutUrl($session, 'http://yourwebsite.com/app/logout.php') . '">Logout</a>'; 
     } else { 
      // no session exists 

      try { 
      $session = $helper->getSessionFromRedirect(); 
      } catch(FacebookRequestException $ex) { 
      // When Facebook returns an error 
      } catch(Exception $ex) { 
      // When validation fails or other local issues 
      echo $ex->message; 
      } 

      echo '<a href="' . $helper->getLoginUrl(array('public_profile', 'email', 'user_friends' , 'publish_stream', 'manage_pages', 'publish_actions', 'status_update', 'user_likes ', 'photo_upload')) . '">Login</a>'; 


     } 



/////////////////////////// 
// SHORT LIVED TOKEN 
    $request = new FacebookRequest($session, "GET", "/".$site["facebook_appid"]."/accounts?access_token=".$site["facebook_appid"]."|".$site["facebook_appsecret"]."&grant_type=client_credentials"); 
    $response = $request->execute(); 
    // get response 
    $graphObject = $response->getGraphObject()->asArray(); 


    if (DebugMode) e($graphObject); 

    $site['facebook_short-lived_access_token'] = $graphObject['data'][0]->access_token; 

//////////////////////// 

/////////////////////////// 
// LONG LIVED TOKEN 
    $request = new FacebookRequest($session, "GET", "/oauth/access_token?grant_type=fb_exchange_token&client_id=".$site["facebook_appid"]."&client_secret=".$site["facebook_appsecret"]."&fb_exchange_token=" .$site['facebook_short-lived_access_token']); 
    $response = $request->execute(); 
    // get response 
    $graphObject = $response->getGraphObject()->asArray(); 


    if (DebugMode) e($graphObject); 

    $site['facebook_long-lived_access_token'] = $graphObject['access_token']; 
    $_SESSION['fb_token'] = $site['facebook_long-lived_access_token']; 

    if (DebugMode) e($site['facebook_long-lived_access_token']); 

?> 

與發佈:

<?php 

require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequest.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/GraphObject.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookSDKException.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequestException.php'); 
require_once(BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookPermissionException.php'); 


use Facebook\FacebookRequest; 
use Facebook\GraphObject; 
use Facebook\FacebookRequestException; 
use Facebook\FacebookSDKException; 
use Facebook\FacebookPermissionException; 


if($session) { 


    try { 
    $response = (new FacebookRequest(
     $session, 'POST', "/".$site["facebook_page_id"]."/feed", 
     array(
     'access_token' => $_SESSION['fb_token'], 
     'link' => $site["url"].'/url', 
     'name' => 'link name', 
     'caption' => 'link caption', 
     'description' => 'link description', 
     'picture' => $site["url"].'/alpha.png', 
     '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(); 

    } 

} 

?> 

我第一次得到一個會話ID,然後請求並獲得一個短暫的令牌,與我請求並獲得一個長期的令牌(60天有效期)。然後我使用最後獲取的令牌繼續發佈腳本。 我提交了一個帖子,它返回了一個後ID,就像它的工作,但沒有在頁面上,沒有在我的個人Facebook歷史。

我的應用處於開發模式,並沒有因Facebook的審查,但它似乎沒有必要了:http://www.nextscripts.com/known-issues/facebook-issues/

任何想法,我錯了?

+0

沒有人可以幫助...這個問題真的沒有任何解決方案嗎? 40天和274的觀點後,仍然沒有答案的陰影... – Matbe81

回答

0

我不是真的到了Facebook SDK,但我注意到一條線,我懷疑是不正確的:

echo '<a href="' . $helper->getLoginUrl(array('public_profile', 'email', 'user_friends' , 'publish_stream', 'manage_pages', 'publish_actions', 'status_update', 'user_likes ', 'photo_upload')) . '">Login</a>'; 

有關該功能的Facebook的文件可以發現here。 它聲明你希望獲得額外的權限,爲數組提供索引'作用域'和作爲值的字符串,並且在你想請求的每個權限之間用逗號代替數組,而索引被調用爲權限但沒有任何數據在裏面。我懷疑它應該是這樣的:

$helper->getLoginUrl(array('scope' => 'public_profile,email,user_friens,publish_stream,<and so on>')); 

我希望這可以幫助你。

相關問題