问题描述:

I intend to create some non-interactive, server-based PHP web logic that retrieves information from Facebook. As a beginner using the Facebook SDK I have problems getting started.

Purely server based, I cannot assume that my users have Facebook accounts, so I want to retrieve the FB information for them - either using a static account or anonymously.

Using the FB dashboard I've created my App ID and App Secret, and installed the SDK facebook-php-sdk-v4-5.0.0

Now, I've trouble getting/understanding my FB access_code right (and probably more than that):

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;

}

It seems I just don't get the access_code right, since whatever I do I get either "Invalid OAuth access token" or "An active access token must be used to query information about the current user"

Any insight is very much appreciated.

Update - this finally worked for me:

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);

网友答案:

This finally worked for me:

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);      
相关阅读:
Top