我的应用主页 URL 顶部有以下内容。
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '123456789',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
FB.getLoginStatus(function(response) {
if(response.status === 'connected') {
if(response.authResponse != 'undefined'){
window.location = '<?php echo $base_url; ?>register';
}
} else if(response.status === 'not_authorized')
{
//it means we have a user but he hasn't granted any permissions to our app
//we're going to redirect him to the permission page
window.location = 'https://www.facebook.com/dialog/oauth?client_id=456109407732505&response_type=code&redirect_uri=<?php echo $base_url; ?>register&scope=email,publish_actions,user_likes';
} else
{
//the user is not logged in, as you already have a login button you don't have to do nothing
}
});
FB.Event.subscribe('auth.login', function(response) {
window.location = '<?php echo $base_url; ?>register';
});
};
(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';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-login-button"
data-show-faces="false"
data-scope="email,publish_actions,manage_notifications,user_likes"
size="large"
data-width="200"
data-max-rows="1">
</div>
此代码引发错误的唯一一次是当查看该站点的用户登录到 FB 但尚未接受应用程序的权限时。在这种情况下,用户将被重定向到 JS 中指定的链接(“ https://www.facebook.com/dialog/oauth?client_id=456109407732505&response_type=code&redirect_uri=register&scope=email,publish_actions,user_likes") 在那里他们可以选择接受或拒绝请求的权限。如果用户选择通过该链接接受权限,那么一切都很好。但是,单击 FB 登录按钮后,将出现一个弹出窗口,这也让用户有机会接受或拒绝权限。在弹出窗口接受请求的权限后,由于某种原因,用户的电子邮件未被获取。用户被重定向到 register.php 文件并抛出错误。但是,当刷新 register.php 页面时,它可以正常工作。
在我的 register.php 文件中。我有
require_once('application/third_party/facebook_api/src/facebook.php');
$facebook = new Facebook(array('appId' => '123456789',
'secret' => 'app_secret',
'cookie' => true));
$access_token = $facebook->getAccessToken();
if($access_token != '')
{
$user = $facebook->getUser();
if($user != 0)
{
// Get the user's info
$user_profile = $facebook->api('/'.$user);
// Get the user's permissions
$perms = $facebook->api('/'.$user.'/permissions');
$perms = $perms['data'][0];
$installed = $perms['installed'];
if($installed == 1)
{
$fb_first_name = $user_profile['first_name'];
$fb_last_name = $user_profile['last_name'];
// Find out if they accepted the email permission
$email_perm = $perms['email'];
if($email_perm == 1)
{
$fb_email = $user_profile['email'];
} else
{
$fb_email = '';
}
}
}
PHP在阅读“未定义索引'电子邮件'”时向我抛出错误。它在刷新页面时起作用,但不是第一次。你可以去我的应用程序查看一个工作示例。
谢谢一堆