簡體   English   中英

在node-webkit應用程序上通過Google登錄時,不會調用gapi.auth.authorize回調

[英]gapi.auth.authorize callback doesn't get called when doing sign-in via Google on node-webkit app

從node-webkit app登錄時出現問題。 在node-webkit應用程序中,我使用以下代碼在我的域上打開一個頁面:

<!DOCTYPE html>
<html>
    <head>
        <meta charset='utf-8' />
        <link rel="stylesheet" src="style.css" />
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript">
             var CLIENT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
             var SCOPE = 'https://www.googleapis.com/auth/userinfo.email';               

            function authorization() {
                   gapi.auth.authorize({
                     client_id: CLIENT_ID,
                     immediate: false,
                     scope: SCOPE
                   }, function(authResult) {
                        alert('CALLBACK');
                      }
                   );
                 }
        </script>       
        <script src="https://apis.google.com/js/client:platform.js?onload=authorization"></script>
        <title>Google Sign In</title>
    </head>
    <body></body>
</html>

出於某種原因,在node-webkit上運行時,回調永遠不會觸發。 在嘗試調試時,我看到了一些奇怪的東西。 當我從node-webkit運行此代碼時,此代碼將打開google登錄屏幕。 當為Google頁面打開node-webkit開發者控制台時,回調會成功觸發。

當我在chrome上加載相同的頁面時,回調會觸發,我可以看到警報,因此我認為問題不在於代碼。 在運行此代碼之前,我以編程方式清理node-webkit緩存,以便每次要求用戶輸入其憑據時。

最終我用不同的方法解決了這個問題。 由於這個問題現在沒有得到答復超過2個月,我將描述我使用過哪種解決方法。

我沒有使用Google js庫進行登錄,而是使用了服務器端身份驗證。 我選擇了Google PHP SDK 這是我使用的流程:

  1. 從node-webkit,我在一個新窗口中從我的服務器打開了一個頁面(php)。

     exports.login_window = window.open(url,{ "position": "center", "focus": true }); 
  2. 使用Google SDK,我生成了一個身份驗證鏈接,並將客戶端重定向到該鏈接。

     $client = new Google_Client(); $client->setClientId($client_id); $client->setClientSecret($client_secret); $client->setRedirectUri($redirect_uri); $client->addScope("https://www.googleapis.com/auth/userinfo.email"); $authUrl = $client->createAuthUrl(); header('Location: '.$authUrl); 
  3. 經過身份驗證后,客戶端會重定向回我的服務器。

  4. 使用谷歌API我查詢了我需要的所有信息。 比我在文件會話上存儲的信息

     "sessionStorage.google_data = ".json_encode($data).";" 
  5. 從原始頁面(當我打開登錄窗口時),我輪詢了新的窗口會話,一旦google_data在那里,我拉動它並關閉窗口。

     if ( typeof exports.login_window == "undefined" || exports.login_window.window == null || typeof exports.login_window.window.sessionStorage == "undefined" || typeof exports.login_window.window.sessionStorage.google_data == "undefined" ) { setTimeout(function(){ check_3p_login(); },200); } else { var google_data = exports.login_window.window.sessionStorage.google_data; // rest of the code } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM