gapi.auth.authorize 在 node-webkit 应用程序上通过 Google 登录时不会调用回调
gapi.auth.authorize callback doesn't get called when doing sign-in via Google on node-webkit app
我在从 node-webkit 应用程序登录时遇到问题。在 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 上 运行ning 时回调永远不会触发。在尝试调试它时,我看到了一些奇怪的东西。当我从 node-webkit 运行 执行此操作时,此代码将打开 google 登录屏幕。当 node-webkit 开发人员控制台为 google 页面打开时,回调成功触发。
当我在 chrome 上加载同一页面时,回调会触发并且我可以看到警报,所以我认为问题不在于代码。在 运行 执行此代码之前,我以编程方式清理 node-webkit 缓存,因此每次用户都需要输入他的凭据。
最终我用不同的方法解决了这个问题。由于这个问题已经有 2 个多月没有得到解答,我将描述我使用的解决方法。
我没有使用 Google js 库进行登录,而是使用了服务器端身份验证。我选择了Google PHP SDK。这是我使用的流程:
在 node-webkit 中,我从我的服务器在新 window.
上打开了一个页面 (php)
exports.login_window = window.open(url,{
"position": "center",
"focus": true
});
使用 Google SDK,我生成了身份验证 link 并将客户端重定向到那个 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);
一旦通过身份验证,客户端就会重定向回我的服务器。
使用google API 我查询了我需要的所有信息。比我将信息存储在文件会话
"sessionStorage.google_data = ".json_encode($data).";"
从原始页面(当我打开登录 window 时),我轮询了新的 window 会话,一旦 google_data 在那里,我拉它并关闭 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
}
我在从 node-webkit 应用程序登录时遇到问题。在 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 上 运行ning 时回调永远不会触发。在尝试调试它时,我看到了一些奇怪的东西。当我从 node-webkit 运行 执行此操作时,此代码将打开 google 登录屏幕。当 node-webkit 开发人员控制台为 google 页面打开时,回调成功触发。
当我在 chrome 上加载同一页面时,回调会触发并且我可以看到警报,所以我认为问题不在于代码。在 运行 执行此代码之前,我以编程方式清理 node-webkit 缓存,因此每次用户都需要输入他的凭据。
最终我用不同的方法解决了这个问题。由于这个问题已经有 2 个多月没有得到解答,我将描述我使用的解决方法。
我没有使用 Google js 库进行登录,而是使用了服务器端身份验证。我选择了Google PHP SDK。这是我使用的流程:
在 node-webkit 中,我从我的服务器在新 window.
上打开了一个页面 (php)exports.login_window = window.open(url,{ "position": "center", "focus": true });
使用 Google SDK,我生成了身份验证 link 并将客户端重定向到那个 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);
一旦通过身份验证,客户端就会重定向回我的服务器。
使用google API 我查询了我需要的所有信息。比我将信息存储在文件会话
"sessionStorage.google_data = ".json_encode($data).";"
从原始页面(当我打开登录 window 时),我轮询了新的 window 会话,一旦 google_data 在那里,我拉它并关闭 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 }