【发布时间】:2016-08-29 08:42:27
【问题描述】:
我已经用 ReactJs 设置了 github 的 Electron。所以我得到了一个BrowserWindow 和一个反应应用程序在那个窗口中运行良好。我想要实现的是通过 GitHub 进行身份验证。因此,当用户按下Login with Github 按钮时,会打开一个新的BrowserWindow 并转到github 授权应用程序网址。我遇到的问题与 回调 以及如何获取回调返回的代码有关。我已经使用 Apache Cordova 和 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 的事情?有什么想法或更好的方法吗?
【问题讨论】:
标签: oauth callback github-api electron