首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法通过WebRequest收听url更改,也无法在电子应用程序上导航

无法通过WebRequest收听url更改,也无法在电子应用程序上导航
EN

Stack Overflow用户
提问于 2021-12-05 15:44:51
回答 1查看 443关注 0票数 0

我正试图在一个电子应用程序上设置OAuth authentication。我想重定向用户到http://localhost:1212/auth/discord?code=xxxxxxxxxxxxxxxx url (电子)后,不和谐授予code。在此之后,我希望侦听URL更改,获取code并进一步进行身份验证。

我试过使用onBeforeRequest of WebRequest

代码语言:javascript
复制
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事件:

代码语言:javascript
复制
authWindow.webContents.on('will-navigate', function (event, newUrl) {
    console.log(newUrl);
    // More complex code to handle tokens goes here
});

问题是,只有在应用程序启动时,代码才能检测到URL的变化。也就是说,console.log(url)在启动时给出了如下内容:

代码语言:javascript
复制
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时,我会得到这样的响应(不是来自onBeforeRequestwill-navigate):

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-14 22:07:24

似乎电子只在源自主电子窗口的窗口内跟踪此类事件。也就是说,如果您只是通过浏览器打开本地主机应用程序,它就不会跟踪事件。

我的解决方案是创建一个特殊的HTTP服务器并将其捆绑到应用程序中。服务器接受OAuth代码是可响应的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70235900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档