更新:我已经能够使网站重定向,但我的电子应用程序尚无法拦截该操作。这在开发中可能吗?我问过这个问题here https://stackoverflow.com/questions/68838012/does-electrons-registerhttpprotocol-work-in-development.
我正在尝试使用电子注册自定义协议。我希望它成为网站可以用来提供 api 密钥的重定向位置(例如myprotocol://example/payload=api-key
).
但是当网站尝试重定向到我的协议时,它一直说:failed to launch myprotocol://example/payload=key... because the scheme does not have a registered handler
。我需要做什么才能使这项工作成功?
我希望能够在生产和开发中实现这一点。
我看过讨论here https://stackoverflow.com/questions/56202191/how-to-integrate-oauth-login-with-a-packaged-electron-app和评论here https://stackoverflow.com/questions/64530295/what-redirect-uri-should-i-use-for-an-authorization-call-used-in-an-electron-app。遵循电子文档here https://www.electronjs.org/docs/api/protocol,这是我尝试过的:
main.js:
const { app, protocol } = require('electron')
app.whenReady().then(() => {
protocol.registerFileProtocol('myprotocol', (request, callback) => {
console.log('It worked! The website is sending this url: ' + request.url)
}, (error) =>{
if (error) console.log('Did not register the protocol')
})
})
重定向 uri 提供给网站:
'myprotocol://example'
我已经能够将其列入白名单myprotocol://example
在网站上。
但是,当网站尝试重定向到此协议时,我的电子应用程序不会注册任何活动,并且网站上的网络检查器显示错误:failed to launch myprotocol://example/payload=key... because the scheme does not have a registered handler
.
我还尝试过其他方法,比如registerSchemesAsPrivileged
and registerHttpProtocol
, 没有成功。
有任何想法吗?