我打算创建一些非交互式,基于服务器的PHP Web逻辑,用于从Facebook检索信息。作为使用Facebook SDK的初学者,我在开始时遇到了问题。
纯粹基于服务器,我不能假设我的用户有Facebook帐户,所以我想为他们检索FB信息 - 使用静态帐户或匿名。
使用FB仪表板我已经创建了我的App ID和App Secret,并安装了SDK facebook-php-sdk-v4-5.0.0
现在,我无法正确获取/理解我的FB access_code(可能还有更多):
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,因为无论我做什么,我都会得到&#34;无效的OAuth访问令牌&#34;或&#34;必须使用活动访问令牌来查询有关当前用户的信息&#34;
非常感谢任何见解。
更新 - 这最终对我有用:
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);
答案 0 :(得分:0)
这最终对我有用:
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);