Facebook PHP SDK V5将用户访问令牌作为页面访问令牌返回

时间:2016-09-04 17:56:21

标签: php facebook facebook-graph-api facebook-php-sdk

我正在尝试使用facebook USER ACCESS TOKEN PAGE ACCESS TOKEN 我已经完成的步骤是

我的申请路径= http://localhost/facebook_req/

  1. 在我的index.php上,我已成功允许我的管理员用户使用以下权限来使用该应用 <?php session_start(); include('Facebook/autoload.php'); $fb = new Facebook\Facebook(array('app_id' => '{appid}', 'app_secret' => '{appsecret}', 'default_graph_version' => 'v2.7')); $helper = $fb->getRedirectLoginHelper(); $permissions = array('publish_pages','manage_pages','pages_show_list'); $loginUrl = $helper->getLoginUrl('http://localhost/facebook_req/callback.php', $permissions); echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';
  2. 2当我点击登录facebook时,我得到了对话框,我允许我允许在callback.php文件中创建的应用程序,我使用了getRedirectLoginHelper(),检索/获取了用户访问权限TOKEN 打印了用户访问令牌&amp;令牌元数据,最后尝试发布到用户帐户并且成功(请参阅以下示例我的令牌元数据的var_dump示例)

    <?php
    session_start();
    include('Facebook/autoload.php');
    $fb = new Facebook\Facebook(array('app_id' => '{appid}',
                                      'app_secret' => '{appsecret}',
                                      'default_graph_version' => 'v2.7'));
    $helper = $fb->getRedirectLoginHelper();
    $permissions = array('publish_pages','manage_pages','pages_show_list');
    $loginUrl = $helper->getLoginUrl('http://localhost/facebook_req/callback.php', $permissions);
    echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';
    
    1. 这部分是奇怪的地方,因为当我试图使用图形资源管理器获取页面访问令牌时,我似乎无法获得 PAGE ACCESS TOKEN 它可以工作,但它对我的代码无效
    2. <code>
      $_SESSION['fb_access_token'] = $accessToken->getValue();
      D:\wamp64\www\facebook_req\callback.php:48:
      object(Facebook\Authentication\AccessTokenMetadata)[13]
        protected 'metadata' => 
          array (size=7)
            'app_id' => string 'xxxxxxxxxxxxxxx' (length=15)
            'application' => string 'xxxxxxxxxxx' (length=8)
            'expires_at' => 
              object(DateTime)[17]
                public 'date' => string '2016-11-03 11:35:38.000000' (length=26)
                public 'timezone_type' => int 3
                public 'timezone' => string 'UTC' (length=3)
            'is_valid' => boolean true
            'issued_at' => 
              object(DateTime)[18]
                public 'date' => string '2016-09-04 11:35:38.000000' (length=26)
                public 'timezone_type' => int 3
                public 'timezone' => string 'UTC' (length=3)
            'scopes' => 
              array (size=5)
                0 => string 'manage_pages' (length=12)
                1 => string 'publish_pages' (length=13)
                2 => string 'pages_show_list' (length=15)
                3 => string 'publish_actions' (length=15)
                4 => string 'public_profile' (length=14)
            'user_id' => string 'xxxxxxxxxxxxxxxx' (length=15)
      </code>
      

      我的php代码返回 USER ACCESS TOKEN 而不是 PAGE ACCESS TOKEN

      ACCESS TOKEN - user access token received from step 2 callback.php file
      METHOD - GET
      FIELDS - nonupublic?fields=access_token
      This works on graph explorer
      

      问题是<code> $fb = new Facebook\Facebook(....); $requestx = $fb->request('GET', '/nonupublic?fields=access_token',array(),$_SESSION['fb_access_token']); var_dump($_SESSION['fb_access_token']); var_dump($requestx); </code> var_dump($_SESSION['fb_access_token']);相同它们没有什么不同,我似乎无法发布到Facebook页面

      顺便说一下,我遵循文档网址指南 - https://developers.facebook.com/docs/pages/access-tokens

      任何人都可以请建议

1 个答案:

答案 0 :(得分:0)

正如@ceejayoz指出我的代码工作我犯了一个错误我已经构建了请求但没有在facebook php v5上执行它我需要执行请求

$response = $fb->getClient()->sendRequest($requestx);

一切都完美无缺