doushao6874 2015-12-29 03:29
浏览 57

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

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/>", "
";
}

$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);      
  • 写回答

1条回答 默认 最新

  • duanjianhe1388 2015-12-30 15:23
    关注

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

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码