简体   繁体   English

如何确定是否存在有效的Facebook访问令牌?

[英]How do I determine if there is an active facebook access token?

I am using the PHP Facebook 3.0 SDK for the graph API and want a simple way to find out if there is currently an active token so that I can handle errors gracefully. 我正在使用用于图形API的PHP Facebook 3.0 SDK,并希望有一种简单的方法来找出当前是否有活动令牌,以便我可以优雅地处理错误。

getUserAccessToken() is a protected method and so I can't seem to access it with the facebook object. getUserAccessToken()是一个受保护的方法,因此我似乎无法通过facebook对象访问它。 There has to be a simple way to do 必须有一个简单的方法

if(active token){ do stuff} else{ don't}

Help anyone? 帮助任何人?

The Facebook PHP SDK Example has all the code you need for this: Facebook PHP SDK示例包含您需要的所有代码:

<?php
require '../src/facebook.php';

$facebook = new Facebook(array(
  'appId'  => '',
  'secret' => '',
));

$user = $facebook->getUser();

if ($user) {
  try {
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
?>

Maybe you can use this type of code: 也许您可以使用以下类型的代码:

$data=file_get_contents('https://graph.facebook.com/me?access_token=xxx');
foreach ($http_response_header as $rh) if (substr($rh, 0, 4)=='HTTP') list(,$status,)=explode(' ', $rh, 3);
if ($status==200 && $data)
    //Token is good, parse data
else
    //Token is no good

Try this: 尝试这个:

try {
    $User = $Facebook->api('/me');
} catch(Exception $e) {
    echo 'No active token.';
    echo $e->getMessage();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM