在我使用facebook sdk登录我的应用程序后,我获得了facebook用户ID,但一段时间后它会自动被破坏。这种情况经常发生。我需要不时地刷新页面。任何人都可以帮我找到解决方案为什么会这样?我正在使用javascript和php sdk。我搜索过相同但我没有得到任何适当的解决方案。
Plz有人帮我这个......
以下是我用来登录的代码:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
function loginfb()
{
FB.login(function(response) {
if (response.authResponse) {
//alert('Success!');
} else {
//alert('Login Failed!');
}
}, {scope: 'manage_pages,publish_actions,email'});
}
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id))
return;
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=<?php echo APP_ID_KEY; ?>";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
这里是我用来获取facebook用户ID的php代码:
require_once('facebook.php');
$facebook = new Facebook(array(
'appId' => APP_ID_KEY,
'secret' => APP_SECRET,
));
$site_url = CALLBACK_URL;
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$friends = $facebook->api('/me/friends');
$token=$facebook->getAccessToken();
} catch (FacebookApiException $e) {
error_log($e);`enter code her`enter code here`e`
$user = null;
}
}
现在,当我第一次登录时,我获得了facebook用户ID但是在一段时间后自动变为零,所以我需要再次点击登录按钮来获取用户ID这种情况非常频繁发生且非常烦人。 。
答案 0 :(得分:0)
PS检查Facebook异常是什么意思
答案 1 :(得分:0)
这是JS方面
window.fbAsyncInit = function() {
FB.init({
appId : app_id,
status : true,
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/pl_PL/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
//callback - function called after succeed login
//opts - The same opts as in FB.login().
function login(callback, opts)
{
//opts must be an object
if (!opts || typeof(opts) !== "object") {
opts = null;
}
FB.getLoginStatus(function(statusResponse) { //check login status
if(statusResponse.status === 'connected') { //user logged in
if(opts !== null && opts.scope !== null) {
//if there is scope opt check that user grant access to all permissions
FB.api('/me/permissions?fields='+opts.scope, 'get', {access_token : statusResponse.authResponse.accessToken }, function(response) {
var perms = opts.scope.split(',');
for(var i=0;i<perms.length;i++) {
if(response.data[0][perms[i]] !== 1) {
//one of permissions fails - do login and break loop
FB.login(function(loginResponse) {
if(loginResponse.status === 'connected')
proceedLogin(loginResponse.authResponse,callback); //ajax call
}, opts);
return;
}
}
//every perm is granted, great so let's proceed user login
proceedLogin(statusResponse.authResponse,callback);//ajax call
});
} else {
//there's no opts or no perms to check
proceedLogin(statusResponse.authResponse,callback);//ajax call
}
} else { //user not logged in :(
FB.login(function(loginResponse) {
if(loginResponse.status === 'connected')
proceedLogin(loginResponse.authResponse,callback);//ajax call
}, opts);
}
});
}
function proceedLogin(authResponse,callback) {
$.post('/ajax/login', authResponse, function(data) {
if (data.user && callback && typeof(callback) === "function") {
callback();
}
});
}
这是PHP方面
function login() {
//FB() your facebook instance
FB()->setAccessToken( $_POST['accessToken'] ); //set access token from JS
FB()->setAccessToken( FB()->setExtendedAccessToken() );
$user = FB()->getUser();
echo json_encode(array( 'user' => $user ));
}