我已经实现的应用程序有一个奇怪的行为,它在访问时捕获用户的信息。如果从 Facebook URL 外部访问该应用程序,则会正确弹出一个 JavaScript 权限对话框。但是,当我将此应用程序作为选项卡应用程序插入 Facebook 页面时,权限请求对话框不再弹出。此外,我还在 FB.getLoginStatus() 实现中放置了检测用户在 Facebook 中的当前登录状态,如果用户未登录,则会弹出一个 JavaScript 警报弹出窗口。当从“外部”调用应用程序时警报被触发。当应用程序位于 Facebook 页面选项卡上时,它不会。这是正确的行为吗?如果是这样,我如何在 Facebook 选项卡应用程序中启用权限请求。一世'
编辑 1(在 CBroe 评论之后):我在应用程序的 content.php 标记中使用以下调用(在页面加载时执行):
<!-- Facebook JS SDK initialization -->
<div id="fb-root"></div>
<script>initFacebookJSSDK();</script>
<!-- End of Facebook JS initialization -->
<script>checkAccessPermission();</script>
<?php include_once 'sites/www.mysite.com/custom_code/custom_index.php';?> // I'm doing
custom code under Drupal
JavaScript 函数在 custom.js 文件中实现,如下所示:
function initFacebookJSSDK() {
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxxxxxxxxxxxx', // Application's ID
channelUrl : 'www.appsite.com/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
oauth : true // enable OAuth
});
.........
});
.........
}
function getPerms() {
FB.login(function(response) {
if (!response.authResponse) {
//user refused to grant permissions, redirect to the 'index' page
window.location = "/index";
}
}, {scope:"email"}); // I request the permission to get the email address
}
function checkAccessPermission() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
//user has granted permissions to the application
var uid = response.authResponse.userID; // not used
var accessToken = response.authResponse.accessToken; // not used
}
else if (response.status === 'not_authorized')
// user is logged into Facebook but didn't grand permissions to the app
getPerms();
else {
//user has not granted permissions, redirect to the 'index' page
alert('Please connect with your Facebook account');
window.location = "/index";
}
});
}
PHP 脚本 - 文件“custom_index.php”包含以下片段:
$config = array();
$config["appId"] = FB_APP_ID;
$config["secret"] = FB_SECRET_ID;
$config["cookie"] = true;
$facebook = new Facebook($config);
// get user UID
$fb_user_id = $facebook->getUser();
// user's first visit, force the permission request window
if ($fb_user_id == 0) {
$location = $facebook->getLoginUrl(array('scope' => 'email'));
..........
}
// user already gave permission, get the profile and process
if ($fb_user_id != 0) {
$access_token = $facebook->getAccessToken();
$user_profile = $facebook->api('/me', array('access_token'=>$access_token));
..........
}
同样,当用户访问 Facebook 之外的站点时,它工作得很好,但是当它作为 Facebook 选项卡应用程序(Facebook 内的框架)运行时,权限请求弹出窗口不显示。需要注意的是,关于我之前的帖子,现在是 JavaScript 声明:
alert('Please connect with your Facebook account');
在这两种情况下都会执行,包括当应用程序在 Facebook 内作为选项卡应用程序运行时(我只需要清除浏览器的缓存)。