Gapi.auth.authorize callback not called when google login in node-webkit app
I have a problem when I login from a node-webkit application. In a node-webkit app, 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. When trying to debug this, I saw something strange. When I run this from node-webkit, this code will bring up the google login screen. When the node-webkit developer console is open to the google page, the callback succeeds.
When I load the same page on chrome, the callback fires and I see a warning, so I don't think the issue is code related. Before running this code, I programmatically clear the node-webkit cache, so every time the user is prompted for their credentials.
source to share
I ended up solving this using a different approach. Since this question has remained unanswered for over 2 months, I will describe which workaround I used.
Instead of signing in with the google js library, I used server side authentication. I chose Google PHP SDK . This is the stream I used:
-
From node-webkit I opened a page (php) from my server in a new window.
exports.login_window = window.open(url,{ "position": "center", "focus": true });
-
using the google SDK I created 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);
-
after authenticating, the client is redirected back to my server.
-
using the google API I requested all the information I needed. Than I saved the information at the session of the document.
"sessionStorage.google_data = ".json_encode($data).";"
-
from the original page (when I opened the login window) I polled the new window session and once google_data was there I pulled it in 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 }
source to share