gpt4 book ai didi

php - Facebook PHP SDK : $facebook->getUser() sometimes returning 0

转载 作者:搜寻专家 更新时间:2023-10-31 21:40:46 25 4
gpt4 key购买 nike

这对我来说是一个非常令人沮丧的问题,因为我无法重现该问题,而且我没有什么可以测试的。出于某种原因,我的 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 ?>

最佳答案

很可能在某些情况下您没有成功验证用户。在某些情况下,用户将使用 PHP SDK 完成初始身份验证过程,但无法收到访问 token 。在这种情况下,将抛出 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?

关于php - Facebook PHP SDK : $facebook->getUser() sometimes returning 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11400235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com