我正试图在一个电子应用程序上设置OAuth authentication。我想重定向用户到http://localhost:1212/auth/discord?code=xxxxxxxxxxxxxxxx url (电子)后,不和谐授予code。在此之后,我希望侦听URL更改,获取code并进一步进行身份验证。
我试过使用onBeforeRequest of WebRequest
session.defaultSession.webRequest.onBeforeRequest(filter, function (details, callback) {
const url = details.url;
// process the callback url and get any param you need
console.log(url);
// don't forget to let the request proceed
callback({
cancel: false
});
});还有will-navigate事件:
authWindow.webContents.on('will-navigate', function (event, newUrl) {
console.log(newUrl);
// More complex code to handle tokens goes here
});问题是,只有在应用程序启动时,代码才能检测到URL的变化。也就是说,console.log(url)在启动时给出了如下内容:
http://localhost:1212/index.html
http://localhost:1212/renderer.dev.js
http://localhost:1212/e227b7535d8985229f67.ttf
http://localhost:1212/561d6473df8929bca8c0.ttf
http://localhost:1212/index.html但是,当我尝试进入http://localhost:1212/auth/discord?code=xxxxxxxxxxxxxxxxxx时,我会得到这样的响应(不是来自onBeforeRequest或will-navigate):
Rewriting GET /auth/discord?code=xxxxxxxxxxxxxxxxxxxxx to /index.html
Rewriting GET /auth/discord?code=xxxxxxxxxxxxxxxxxxxxx to /index.html
Rewriting GET /auth/discord?code=xxxxxxxxxxxxxxxxxxxxx to /index.html根本没有检测到访问http://localhost:1212/index.html。我在用electron-react-boilerplate。
发布于 2021-12-14 22:07:24
似乎电子只在源自主电子窗口的窗口内跟踪此类事件。也就是说,如果您只是通过浏览器打开本地主机应用程序,它就不会跟踪事件。
我的解决方案是创建一个特殊的HTTP服务器并将其捆绑到应用程序中。服务器接受OAuth代码是可响应的。
https://stackoverflow.com/questions/70235900
复制相似问题