使用PHP sdk获取新access_token的最佳方法

时间:2012-09-08 19:58:17

标签: php facebook-graph-api facebook-php-sdk

我收到错误 “必须使用活动访问令牌来查询有关当前用户的信息”。 似乎有大量的文章和想法,但我很困惑,似乎事情也在发生变化。很多人说要使用离线访问,但似乎已经消失了。 我找到了this article

有没有人有使用PHP SDK的示例?

我尝试过像下面这样的东西,但它似乎没有用; $FBuser仍为零:

$token_url =    "https://graph.facebook.com/oauth/access_token?" .
                            "client_id=" . FB_APP_ID .
                            "&client_secret=" . FB_APP_SECRET .
                            "&grant_type=client_credentials";
list($name, $ACCESS_TOKEN) = explode("=", file_get_contents($token_url) );
$facebook->setAccessToken(ACCESS_TOKEN);
$FBuser = $facebook->getUser();

1 个答案:

答案 0 :(得分:0)

尝试此操作(根据此link更改范围参数):

// Checks if the user granted the access (after the redirection bellow)
$user = $facebook->getUser();
if(!$user) {
    // If not: Retrieves the login url in order to redirect the user
    $url = $facebook->getLoginUrl(array('scope' => 'user_about_me', 'grant_type' => 'client_credentials'));
    // redirect here (after accepting the user will be redirect back to the same url. In other words this script, that will check again if the user is authenticated)
} else {
    // Now the user is authenticated
    $user_data  = $facebook->api('/me');
    $user_token = $facebook->getAccessToken();
    // Saves the access token
}