2012-06-02 38 views
0

我有一個奇怪的行爲,我已經實現了一個應用程序,並在訪問時捕獲用戶的信息。 如果應用程序從Facebook URL外部訪問,則會正確彈出JavaScript權限對話框。 但是,當我將此應用程序作爲選項卡應用程序插入Facebook頁面時,許可請求對話框不再彈出。此外,我還在FB.getLoginStatus()實現中檢測用戶當前在Facebook中的登錄狀態,如果用戶未登錄,則會彈出一個JavaScript警報窗口。當應用程序從「外部」警報被觸發。當應用程序在Facebook頁面選項卡上時,它不會。 這是正確的行爲?如果是這樣,我如何在Facebook選項卡應用程序中啓用權限請求。我正在使用JS和PHP Facebook SDK。用戶權限對話框不彈出標籤應用程序

編輯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功能自定義實現。 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標籤的應用程序(我剛剛清除瀏覽器緩存),包括。

+0

你能告訴我們你的代碼的相關部分嗎? – CBroe

回答

1

這是解決方案。 以上所有代碼都是正確的,並保持原樣。 我剛剛在鏈接上添加了一個jQuery函數,如下所示(該類'.open-content-page'位於'content.php'的父頁面上):

$(document).ready(function() { 
      $(".open-content-page").click(function() { 
      checkAccessPermission(); 
      }); 
}); 
})(jQuery); 
0

你在沒有任何用戶交互的情況下調用了你的JS函數 - 你確定它不僅僅是你的瀏覽器的彈出窗口阻止程序讓對話框不再出現嗎? (也許是區分彈出窗口從「簡單」的網頁與彈出式廣告被稱爲從嵌入iframe中的其他領域頁面之間)。

既然你不能依賴於具有他們彈出窗口攔截你的網站的用戶設置非常寬鬆,這是一般來說,更好的想法是在用戶交互上調用facebook對話框當用戶點擊鏈接/按鈕時。調用用戶交互的彈出窗口比想要單獨打開所有窗口的彈出窗口不太可能被阻塞。

+0

事實上,JavaScript警報彈出窗口(我提到的)在這兩種情況下都會顯示瀏覽器彈出窗口阻止。此外,爲了測試,我啓用了facebook.com域的彈出窗口,但權限對話框仍然不顯示 –

+0

不,警告通知與彈出窗口不同。試圖打開彈出窗口的域名不是facebook.com,而是您的域名,因爲這是iframe中的文檔已被加載的地方。 – CBroe

+0

我在發佈之前做了一些測試。我知道你提到並通過相同邏輯傳遞的問題,但這裏是關於Facebook API,更不用說推理不幸;例如:在進行測試時,我強制使用了一個彈出塊ALERT,然後檢查了允許的域列表 - 嗯,它是... facebook.com!無論如何,我認爲解決方案是強制PHP代碼的權限對話框。你有建議的基礎 –

相關問題