PHP Facebook SDK 4.0 作为页面服务器发布到服务器

Posted

技术标签:

【中文标题】PHP Facebook SDK 4.0 作为页面服务器发布到服务器【英文标题】:PHP Facebook SDK 4.0 post as a page server to server 【发布时间】:2014-05-20 07:46:02 【问题描述】:

自从 6 天以来,我一直在尝试在 Facebook 页面上作为页面发布,但没有成功,使用新的 facebook SDK 4.0.4 真的很令人沮丧。

我在 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/

知道我哪里错了吗?

【问题讨论】:

没有人可以帮忙...这个问题真的没有解决办法吗? 40 天和 274 次查看后,仍然没有答案的影子...... 【参考方案1】:

我不是很喜欢 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>'));

希望对你有帮助。

【讨论】:

以上是关于PHP Facebook SDK 4.0 作为页面服务器发布到服务器的主要内容,如果未能解决你的问题,请参考以下文章

Facebook PHP SDK 4.0 使用 Lumen 登录

如何使用php SDK在一个域内托管Facebook(作为App)运行php代码? [关闭]

ParseFacebookUtilsV4 和 Facebook-iOS-SDK 4.0

Android Facebook SDK 4.0 登录:用户登录 Facebook 应用程序时出错

Facebook iOS SDK > 4.0 替代 initWithAppId?

iOS Facebook sdk 4.0 分享图片