douhandie6615 2012-07-28 23:04
浏览 71
已采纳

Facebook SDK getAccessToken()问题

I have been having some trouble with the facebook SDK, specifically retrieving an access token. The App is set up on Facebook correctly and has been given permission. As far as I can tell, the code is correct and I am not sure what is going wrong with getAccessToken().

$facebook->getAccessToken(); is returning "12345678|abcdefghijklmnop", basically some kind of variable which is made up of the App ID and Secret ID separated by a |

$facebook->getUser(); is returning '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();
?>

Thank you in advance!

  • 写回答

2条回答 默认 最新

  • douxueke5653 2012-07-28 23:16
    关注

    From 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;
      }
    

    Your access token will be APP ID|APP SECRET which is the application token, or A RANDOM TOKEN HERE, which is the user access token, when you have a user signed:

    $facebook->getUser();
    

    When getUser() return 0 (which is same as false, and user not signed) you need to request a login, and authorization (if have not authorized yet) for your application:

    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;
    }
    

    see available permissions types here: https://developers.facebook.com/docs/authentication/permissions/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作