dsh7551 2012-07-09 17:40
浏览 66
已采纳

Facebook PHP SDK:$ facebook-> getUser()有时返回0

This is a very frustrating problem for me as I am unable to recreate the problem and I have nothing to test off of. For some reason random users of my Facebook app will have their UID returned as 0. It doesn't appear to be a specific browser issue from what I can tell.

Here's how my app works. I have a homepage where a user can click and Enter Now link. This button triggers the oauth(); JS function and should pop open the OAuth login window. See my JS code below..

window.fbAsyncInit = function() {
FB.init({
  appId      : 'xxxxxxxxxxxxx', // App ID
  channelUrl : '//channel.html', // Channel File
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true  // parse XFBML
});


FB.Canvas.scrollTo(0,0); //makes window scroll to top when loading page within iframe
FB.Canvas.setAutoGrow(); //resize the height of the iframe automatically
};

// Load the SDK Asynchronously
(function(d){
 var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
 if (d.getElementById(id)) {return;}
 js = d.createElement('script'); js.id = id; js.async = true;
 js.src = "//connect.facebook.net/en_US/all.js";
 ref.parentNode.insertBefore(js, ref);
}(document));

function oauth() {   
   FB.login(function(response) {
   if (response.authResponse) {
     console.log('Welcome!  Fetching your information.... ');

     FB.api('/me', function(response) {
       console.log('Good to see you, ' + response.name + '.');
       //window.location.reload();
       //alert("bing bong bing");
       $.fancybox.open("#entry-form", {
           width: 645,
           minHeight: 920,
           padding: 0,
           scrolling: 'no',
           wrapCSS: 'enternow-form',
           type: 'iframe',
           href: 'upload',
           helpers : {
               overlay : {
                   opacity: 0.95,
                   css : {
                   'background-color' : '#fff'
                    }
    }
}              
       });
     });
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 });
}//end oauth function

If the oauth login is successful I pop open a FancyBox iframe that has a form in it. Here is the Facebook PHP code on the iframe page to get the UID...

<?php header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'); ?>
<?php session_start(); ?>
<?php require_once("../src/facebook.php"); ?>
<?php
$config = array();
$config["appId"] = 'xxxxxxxxxxxxxx';
$config["secret"] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$config["fileUpload"] = false; // optional

$facebook = new Facebook($config);

// get signed request to help determine if user is an admin
$signedRequest = $facebook->getSignedRequest();

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
 try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
 } catch (FacebookApiException $e) {
error_log($e);
$user = null;
 }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
?>

<?php echo $user //this is the user's ID ?>
  • 写回答

1条回答 默认 最新

  • douhai5835 2012-07-09 20:14
    关注

    Most likely, you have not successfully authenticated the user in certain circumstances. There are occassions where the user will go through the initial authentication process with the PHP SDK but fail to receive an access token. In this case, FacebookApiException is thrown.

    if ($user) {
        try {
            // Proceed knowing you have a logged in user who's authenticated.
            $user_profile = $facebook->api('/me');
        } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }
    }
    

    Reference: How to properly handle session and access token with Facebook PHP SDK 3.0?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?