我用ReactJ设置了github的电子。所以我有一个BrowserWindow和一个react应用程序在那个窗口里玩得很好。我想要实现的是使用GitHub进行身份验证。因此,当用户按下Login with Github按钮时,会打开一个新的BrowserWindow并转到github授权应用程序url。我的问题与回调有关,以及如何从回调中获得返回的代码。我已经在Apache和InAppBrowser中完成了这一工作,但是由于我能够使用localhost作为回调,所以情况就不同了。
到目前为止,我对电子所做的是打开新的BrowserWindow,但是在授权之后,我无法从回调中获得代码。
var authWindow = new BrowserWindow({ width: 800, height: 600, show: true, 'always-on-top': true });
var githubUrl = 'https://github.com/login/oauth/authorize?';
var authUrl = githubUrl + 'client_id=' + options.client_id + '&scope=' + options.scope;
authWindow.loadUrl(authUrl);
authWindow.setVisibleOnAllWorkspaces(true);
authWindow.setResizable(false);
authWindow.addListener('page-title-updated', function(stream) {
console.log("LOADED");
console.log(JSON.stringify(stream));
console.log(stream);
var url = (typeof stream.url !== 'undefined' ? stream.url : stream.originalEvent.url),
raw_code = /code=([^&]*)/.exec(stream.url) || null,
code = (raw_code && raw_code.length > 1) ? raw_code[1] : null,
error = /\?error=(.+)$/.exec(strean.url);
if (code || error) {
authWindow.close();
}
// If there is a code in the callback, proceed to get token from github
if (code) {
// requestToken(code);
} else if (error) {
alert("Oops! Couldn't log authenticate you with using Github.");
}
});在我做console.log(JSON.stringify(stream));的地方,我得到了{},所以它必须要做eventListener?有什么主意或者更好的方法吗?
发布于 2015-06-01 22:21:15
所以我错过的是正确的event。正确的做法是:
// Build the OAuth consent page URL
var authWindow = new BrowserWindow({ width: 800, height: 600, show: false, 'node-integration': false });
var githubUrl = 'https://github.com/login/oauth/authorize?';
var authUrl = githubUrl + 'client_id=' + options.client_id + '&scope=' + options.scopes;
authWindow.loadUrl(authUrl);
authWindow.show();
// Handle the response from GitHub
authWindow.webContents.on('did-get-redirect-request', function(event, oldUrl, newUrl) {
var raw_code = /code=([^&]*)/.exec(newUrl) || null,
code = (raw_code && raw_code.length > 1) ? raw_code[1] : null,
error = /\?error=(.+)$/.exec(newUrl);
if (code || error) {
// Close the browser if code found or error
authWindow.close();
}
// If there is a code in the callback, proceed to get token from github
if (code) {
requestGithubToken(options, code);
} else if (error) {
alert("Oops! Something went wrong and we couldn't log you in using Github. Please try again.");
}
});
// Reset the authWindow on close
authWindow.on('close', function() {
authWindow = null;
}, false);我还编写了一个教程,其中描述了在http://manos.im/blog/electron-oauth-with-github/中可以找到的完整实现
https://stackoverflow.com/questions/30281732
复制相似问题