简体   繁体   中英

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

I have an issue when doing sign-in from node-webkit app. In the node-webkit application, I open a page on my domain with the following code:

<!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>

For some reason the callback never fires when running on node-webkit. While trying to debug it I saw something strange. When I run this from node-webkit, this code will open the google sign-in screen. When the node-webkit developer console is open for the google page, the callback fires successfully.

When I load the same page on chrome, the callback fires and I can see the alert, so I don't think the issue is with the code. Before running this code I programmatically clean the node-webkit cache so each time the user is required to enter his credentials.

Eventually I solved this using a different approach. Since this question was left unanswered for over 2 months now, I will describe which workaround I've used.

Instead of doing the sign-in using Google js library, I used server side authentication. I chose the Google PHP SDK . This is the flow I used:

  1. From node-webkit, I opened a page (php) from my server on a new window.

     exports.login_window = window.open(url,{ "position": "center", "focus": true }); 
  2. using Google SDK, I generated an authentication link and redirected the client to that link.

     $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. once authenticated, the client redirected back to my server.

  4. using google API I queried for all the information I needed. Than I stored the information on the document session

     "sessionStorage.google_data = ".json_encode($data).";" 
  5. from the original page (when I opened the login window), I polled the new window session, and once the google_data was there, I pulled it and closed the window.

     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 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM