使用 PHP sdk 进行应用程序授权

发布于 2024-12-02 17:13:31 字数 817 浏览 0 评论 0原文

我正在使用 最新 Facebook PHP SDK 并且想要简单地访问用户数据:

$facebook = new Facebook(array(  
    'appId'  => 'xxx',  
    'secret' => 'yyy',  
    'cookie' => true  
)); 
$user = $facebook->getUser();
  if ($user) { // Checks if there is already a logged in user
  try {
    // Proceed knowing you have a logged in user who's already authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
   }
}
else { 
   //Ask for bare minimum login
   $login_url = $facebook->getLoginUrl();
   header("Location: ".$login_url); 
}

我的问题是$user 已登录 FB,但 $facebook->api('/me') 不仅失败,而且我没有收到应用程序授权对话框任何一个。 我做错了什么?

I'm using the latest Facebook PHP SDK and want to simply access user data:

$facebook = new Facebook(array(  
    'appId'  => 'xxx',  
    'secret' => 'yyy',  
    'cookie' => true  
)); 
$user = $facebook->getUser();
  if ($user) { // Checks if there is already a logged in user
  try {
    // Proceed knowing you have a logged in user who's already authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
   }
}
else { 
   //Ask for bare minimum login
   $login_url = $facebook->getLoginUrl();
   header("Location: ".$login_url); 
}

My problem is that the $user is FB logged in, but $facebook->api('/me') not only fails, but I don't get the App authorization dialog either.
What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乱世争霸 2024-12-09 17:13:31

最终发现我的错误,所以记录一下:

$facebook = new Facebook(array(  
    'appId'  => 'xxxx',  
    'secret' => 'yyyyy',  
    'cookie' => true  
 )); 
$user = $facebook->getUser();
  if ($user) { // Checks if there is already a logged in user
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    // Will throw the very first time as the token is not valid
    error_log($e);
    $user = null;
   }
}
// $user is null : $user is either not logged in or the token is not valid
if(!$user) { //Ask for bare minimum login
    $login_url = $facebook->getLoginUrl();
   header("Location: ".$login_url); 
}

Eventually found my error so for the record:

$facebook = new Facebook(array(  
    'appId'  => 'xxxx',  
    'secret' => 'yyyyy',  
    'cookie' => true  
 )); 
$user = $facebook->getUser();
  if ($user) { // Checks if there is already a logged in user
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    // Will throw the very first time as the token is not valid
    error_log($e);
    $user = null;
   }
}
// $user is null : $user is either not logged in or the token is not valid
if(!$user) { //Ask for bare minimum login
    $login_url = $facebook->getLoginUrl();
   header("Location: ".$login_url); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文