所以我必须通过单击进入按钮来请求扩展权限,但是登录后需要刷新页面才能显示应用程序。这是我的代码:
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => '< THE APPID >',
'secret' => '< THE SECRET >',
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '< THE APPID >',
status : true,
cookie : true,
xfbml : true
});
// Additional initialization code such as adding Event Listeners goes here
$('#btn-enter').click(function(){
login();
});
};
(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 login() {
FB.login(function(response) {
if (response.authResponse) {
// connected
} else {
// cancelled
}
//});
}, {scope: 'read_friendlists,friends_photos,publish_stream'});
}
</script>
<?php if ($user): ?>
<!--Here is my APP-->
<?php else: ?>
<a id="btn-enter">Enter</a>
<?php endif ?>
有一个更好的方法吗 ?对我有用的是:
function login() {
FB.login(function(response) {
if (response.authResponse) {
top.location.href='https://the_app_url';
} else {
}
//});
}, {scope: 'read_friendlists,friends_photos,publish_stream'});
}
但这会导致整个页面刷新并且本身并不“优雅”......