[英]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. 从node-webkit app登录时出现问题。 In the node-webkit application, I open a page on my domain with the following code: 在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>
For some reason the callback never fires when running on node-webkit. 出于某种原因,在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. 当我从node-webkit运行此代码时,此代码将打开google登录屏幕。 When the node-webkit developer console is open for the google page, the callback fires successfully. 当为Google页面打开node-webkit开发者控制台时,回调会成功触发。
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. 当我在chrome上加载相同的页面时,回调会触发,我可以看到警报,因此我认为问题不在于代码。 Before running this code I programmatically clean the node-webkit cache so each time the user is required to enter his credentials. 在运行此代码之前,我以编程方式清理node-webkit缓存,以便每次要求用户输入其凭据时。
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. 由于这个问题现在没有得到答复超过2个月,我将描述我使用过哪种解决方法。
Instead of doing the sign-in using Google js library, I used server side authentication. 我没有使用Google js库进行登录,而是使用了服务器端身份验证。 I chose the Google PHP SDK . 我选择了Google PHP SDK 。 This is the flow I used: 这是我使用的流程:
From node-webkit, I opened a page (php) from my server on a new window. 从node-webkit,我在一个新窗口中从我的服务器打开了一个页面(php)。
exports.login_window = window.open(url,{ "position": "center", "focus": true });
using Google SDK, I generated an authentication link and redirected the client to that link. 使用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);
once authenticated, the client redirected back to my server. 经过身份验证后,客户端会重定向回我的服务器。
using google API I queried for all the information I needed. 使用谷歌API我查询了我需要的所有信息。 Than I stored the information on the document session 比我在文件会话上存储的信息
"sessionStorage.google_data = ".json_encode($data).";"
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. 从原始页面(当我打开登录窗口时),我轮询了新的窗口会话,一旦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.