Facebook SDK;PHP-服务器端身份验证和访问


Facebook SDK & PHP - Server side authentication and access

我打算创建一些非交互式的、基于服务器的PHP web逻辑,用于从Facebook检索信息。作为一个使用Facebook SDK的初学者,我在入门时遇到了一些问题。

纯粹基于服务器,我不能假设我的用户有Facebook帐户,所以我想为他们检索FB信息——要么使用静态帐户,要么匿名。

使用FB面板,我创建了我的应用程序ID和应用程序秘密,并安装了SDK facebook-php-SDK-v4-5.0.0

现在,我很难正确理解我的FB访问代码(可能更多):

define( 'APP_NAME',         "xxxxx" ); //       
define( 'APP_ID',           "19427xxxxxxx" );
define( 'APP_VERSION',      "v2.5" );
define( 'APP_SECRET',       "8c31fxxxxxxx" );
session_start();
$fb = new Facebook'Facebook( [
  'app_id'                  => APP_ID,
  'app_secret'              => APP_SECRET,
  'default_graph_version'   => APP_VERSION,
] );
// get oauth token ??
$query = http_build_query([
     'client_id'     => APP_ID,
     'client_secret' => APP_SECRET,
     'grant_type'    => 'client_credentials',
]);
$response1 = $fb->get( '/oauth/access_token?' . $query, $fb->getApp()->getAccessToken() );  
$graph = $response1->getGraphObject();      
foreach( $graph->getPropertyNames() as $prop)
{
    echo  $prop, "=", $graph->getProperty( $prop ), "<br/>", "'n";
}
$access_token = $graph->getProperty( 'access_token' );
$fb->setDefaultAccessToken( $access_token );
$request = new Facebook'FacebookRequest( $fb->getApp(), $access_token, 'GET', '/me?fields=id,name');
// Send the request to Graph
try
{
    $response = $fb->getClient()->sendRequest($request);
}
catch (Facebook'Exceptions'FacebookResponseException $e)
{
    echo 'Graph returned an error: ' . $e->getMessage();    
    // --> Invalid OAuth access token.
    // --> An active access token must be used to query information about the current user
    exit;
}

我似乎只是没有得到正确的access_code,因为无论我做什么,我都会得到"无效的OAuth访问令牌"或"必须使用活动的访问令牌来查询有关当前用户的信息"

任何见解都将不胜感激。

更新-这对我来说终于奏效了:

session_start();
$fb = new Facebook'Facebook( [
  'app_id'                  => APP_ID,
  'app_secret'              => APP_SECRET,
  'default_graph_version'   => APP_VERSION,
] );
// get oauth token ??
$query = http_build_query([
     'client_id'     => APP_ID,
     'client_secret' => APP_SECRET,
     'grant_type'    => 'client_credentials',
]);
$r_token    = $fb->get( '/oauth/access_token?' . $query, $fb->getApp()->getAccessToken() ); 
$graph= $r_token->getGraphObject();     
$a_token= $graph->getProperty( 'access_token' );
$fb->setDefaultAccessToken( $a_token );
$since = strtotime($since_date);
$until = strtotime($until_date);        
$fields=    "id,name,description,place,timezone,start_time,end_time";
$json_link= "https://graph.facebook.com/" . PAGE_ID . "/events/attending/?access_token={$a_token}&fields={$fields}&since={$since}&until={$until}";
$json = file_get_contents($json_link);      

这对我来说终于奏效了:

session_start();
$fb = new Facebook'Facebook( [
  'app_id'                  => APP_ID,
  'app_secret'              => APP_SECRET,
  'default_graph_version'   => APP_VERSION,
] );
// get oauth token ??
$query = http_build_query([
     'client_id'     => APP_ID,
     'client_secret' => APP_SECRET,
     'grant_type'    => 'client_credentials',
]);
$r_token    = $fb->get( '/oauth/access_token?' . $query, $fb->getApp()->getAccessToken() ); 
$graph= $r_token->getGraphObject();     
$a_token= $graph->getProperty( 'access_token' );
$fb->setDefaultAccessToken( $a_token );
$since = strtotime($since_date);
$until = strtotime($until_date);        
$fields=    "id,name,description,place,timezone,start_time,end_time";
$json_link= "https://graph.facebook.com/" . PAGE_ID . "/events/attending/?access_token={$a_token}&fields={$fields}&since={$since}&until={$until}";
$json = file_get_contents($json_link);