2013-10-11 13 views
0

什麼會我需要做的,允許外部網站手機和Facebook標籤應用程序之間的異步登錄/認證(權限)。PHP和Javascript異步的權限認證 - Facebook的API的

我基本上需要一個系統,不能既桌面/移動。

案例1(移動/外部訪問) - 日誌中使用Javascript使用登錄和工作正常應用完美地工作。 - 這種方式很有效!我的問題是JavaScript知道用戶已經登錄,但仍然要求用戶登錄以獲得權限(如果用戶已經登錄)他們已經給出了。

TLDR:爲什麼Javascript不能從PHP SDK獲取身份驗證cookie並認識到用戶具有PHP的正確權限?當用戶通過手機或外部登錄到應用程序時,此登錄流程正常。我試過他們,他們工作得很好,但不會一起工作。

我已經看到了各地工作的其他方式,但我想只有JS或PHP> JS不是JS> PHP。

*編輯

我的事件處理程序的登錄/註銷,喜歡到這裏。

(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 fblogin() 
{ 
    FB.login(function(response) 
    { 
    }, 
    { scope: 'email, user_likes' } 
    ); 
} 

function createRequestObject() { 
    var obj; 
    var browser = navigator.appName; 
    if(browser == "Microsoft Internet Explorer") 
    { 
     obj = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    else 
    { 
     obj = new XMLHttpRequest(); 
    } 
    return obj; 
} 

登錄移動/桌面的偉大工程,它拿起範圍,但是當我使用下面的代碼來設置標籤的應用程序,權限將不會轉移到JavaScript的會話。

PHP代碼(工作):

<?php 
require_once 'src/facebook.php'; // get facebook sdk 
$facebook = new Facebook(array(
    'appId' => 'APP ID HERE', 
    'secret' => 'APP SECRET HERE' 
)); 

$user = $facebook->getUser(); 
$location = "". $facebook->getLoginUrl(array('scope' => 'email, user_likes')); 

// check if we have valid user 
if ($user) { 
    try { 
     // Proceed knowing you have a logged in user who's authenticated. 
     $permissions = $facebook->api('/me/permissions', 'get', array('access_token'=>$access_token)); 
    } catch (FacebookApiException $e) { 
     $fb_user_id = NULL; 
     // seems we don't have enough permissions 
     // we use javascript to redirect user instead of header() due to Facebook bug 
     print '<script language="javascript" type="text/javascript"> top.location.href="' . $location .'"; </script>'; 

     // kill the code so nothing else will happen before user gives us permissions 
     echo $e->getMessage(); 
     die(); 
    } 
    } else { 
     // seems our user hasn't logged in, redirect user to a FB login page 
     print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>'; 
    // kill the code so nothing else will happen before user gives us permissions 
    die(); 
} 

// at this point we have an logged in user who has given permissions to our APP 
// Facebook opens canvas page (which is the mobile/external page) but doesn't transfer permissions. 

PHP的偉大工程,它的用戶移動(畫布)頁/傳輸與登錄類似按鈕。我如何讓PHP將我的手機/桌面網站所在的畫布傳遞給我的畫布?

* 編輯2: HTML/PHP:

<div class="container"> 
<div id="fb-root"></div> 
<div id="facebook-div"> 
    <fb:login-button autologoutlink="true" scope="email,user_likes" size="large"></fb:login-button> 
    <div id="facebook-right" class="fb-like" data-href="https://www.facebook.com/154456204613952" data-width="90" data-layout="button_count" data-show-faces="false" data-send="false"></div> 
</div> 
//Rest of the code here 
+0

你必須讓你的代碼是什麼樣子,尤其是頁面標籤應用中的JS部分更具體一點。 – CBroe

+0

謝謝CBroe - 我認爲啓用JavaScript內的cookie會起作用,或者只有您通過JavaScript首先登錄才能使用此係統。 – Samuel

+0

不,它應該雙向工作。但我仍然沒有看到你實際上在哪裏調用了你的JS登錄函數,在什麼條件下你做或不叫它。 – CBroe

回答

0

管理來解決它感謝您的幫助! PHP的實例後,我並沒有包括畫布PHP文件的用戶被驗證之後......我一直在拉我的頭髮好幾個月試圖解決這個!這個認證被傳遞給Javascript,它處理所有事情!

這意味着,PHP和JavaScript調用都能夠在同一應用程序內工作。

相關問題