3

這是一個非常令人沮喪的問題,因爲我無法重新創建問題,我沒有什麼可以測試的。出於某種原因,我的Facebook應用程序的隨機用戶將他們的UID返回爲0.它看起來並不是我所知道的特定瀏覽器問題。

下面是我的應用程序的工作原理。我有一個主頁,用戶可以點擊並立即輸入鏈接。此按鈕會觸發oauth(); JS功能,並應彈出OAuth登錄窗口。看到我下面的JS代碼..

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 

如果oauth登錄成功,我彈出一個FancyBox iframe,其中有一個窗體。這裏的iframe頁面獲得UID Facebook上的PHP代碼...

<?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

最有可能的,你有沒有成功驗證在某些情況下用戶。有些場合用戶將通過PHP SDK進行初始身份驗證過程,但無法接收訪問令牌。在這種情況下,會拋出FacebookApiException

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

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