Facebook SDK getAccessToken()问题

时间:2012-07-28 23:04:44

标签: php facebook sdk access-token

我在facebook SDK上遇到了一些麻烦,特别是检索了访问令牌。该应用程序已在Facebook上正确设置并已获得许可。据我所知,代码是正确的,我不确定getAccessToken()出了什么问题。

$ facebook-> getAccessToken(); 正在返回“12345678 | abcdefghijklmnop”,基本上是某种由App ID和Secret ID组成的变量由|

分隔

$ facebook-> getUser(); 正在返回' 0 '

<?php
     require_once("facebook.php"); //Up-to-date SDK files from Git

     $app_id = "12345678"; //replaced with fake
     $app_secret = "abcdefghijklmnop"; //replaced with fake

     $facebook = new Facebook(array(
         'appId' => $app_id,
         'secret' => $app_secret,
         'cookie' => true; //I have tried 'false' here as well
     ));

     $token = $facebook->getAccessToken();
?>

提前谢谢!

2 个答案:

答案 0 :(得分:2)

$facebook->getAccessToken();正在返回"12345678|abcdefghijklmnop",基本上是某种变量,它由以|

分隔的App ID和Secret ID组成

答案 1 :(得分:1)

来自Facebook SDK:

  public function getAccessToken() {
    if ($this->accessToken !== null) {
      return $this->accessToken;
    }

    $this->setAccessToken($this->getApplicationAccessToken());
    $user_access_token = $this->getUserAccessToken();
    if ($user_access_token) {
      $this->setAccessToken($user_access_token);
    }

    return $this->accessToken;
  }

  protected function getApplicationAccessToken() {
    return $this->appId.'|'.$this->appSecret;
  }

当您有用户签名时,您的访问令牌将是APP ID|APP SECRET应用程序令牌,或A RANDOM TOKEN HERE,即用户访问令牌:

$facebook->getUser();

getUser()返回0(与false相同且用户未签名)时,您需要为您的申请申请登录和授权(如果尚未授权) :

if (!$facebook->getUser())
{
    $login_url = $facebook->getLoginUrl(array(
            'scope' => 'publish_stream' // Permissions goes here
        ) 
    );
?>
    <script type="text/javascript">
        top.location.href = " <?php echo $login_url; ?>";
    </script>
<?php
    exit;
}

在此处查看可用的权限类型:https://developers.facebook.com/docs/authentication/permissions/