【问题标题】:Facebook OAUTH token is always NULLFacebook OAUTH 令牌始终为 NULL
【发布时间】:2012-12-12 15:07:17
【问题描述】:

每当我向 Facebook 发送令牌请求时,我都会收到 NULL 响应。不是异常或成功响应。永远NULL

$this->facebook = new Facebook(array(
    'appId'  => 'APP ID', 
    'secret' => 'APP SECRET',
    'cookie' => true
));

if(isset($_GET['code'])) {
    $token = $this->facebook->api('/oauth/access_token', 'GET', array(
        'client_id' => 'APP ID',
        'client_secret' => 'APP SECRET',
        'redirect_uri' => 'http://mywebsite.com?route=' . urlencode('account/connect/facebook/'),
        'code' => $_GET['code']
    ));

    print_r($token); // NULL
    exit;
} else {
    $this->redirect($this->facebook->getLoginUrl(array(
                'redirect_uri' => 'http://mywebsite.com?route=' . urlencode('account/connect/facebook/')
            )
        )
    );
}

【问题讨论】:

    标签: php facebook oauth facebook-oauth


    【解决方案1】:
    $token = $this->facebook->api('/oauth/access_token',
    

    您不能使用 Facebook::api 进行此调用,因为该方法需要 JSON 响应,而此端点响应该响应。

    但是你为什么要这样做呢? PHP SDK 已经具有检测传递的code 参数并将其交换为包含的访问令牌的功能,并且会自动执行。

    【讨论】:

      【解决方案2】:

      我认为你可以这样做:

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

      http://developers.facebook.com/docs/reference/php/facebook-getAccessToken/

      【讨论】:

      • 不,因为我会得到:Uncaught OAuthException: An active access token must be used to query information about the current user,因为默认访问令牌是这样的APPID|APPSECRET
      猜你喜欢
      • 1970-01-01
      • 2011-02-11
      • 2017-06-09
      • 2021-08-08
      • 2012-01-29
      • 2012-05-23
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多