Javascript 处理来自electron(或其他桌面平台)的oauth2重定向

Javascript 处理来自electron(或其他桌面平台)的oauth2重定向,javascript,oauth-2.0,electron,Javascript,Oauth 2.0,Electron,这主要是因为缺乏对oauth2的理解,可能并不是electron特有的,但是我想了解一下有人会如何处理来自桌面平台的oauth2重定向url,比如electron 假设应用程序中没有webservice设置,桌面应用程序将如何提示用户提供针对第三方oauth2服务的凭据,然后正确验证这些凭据?Electron JS在本地主机上运行浏览器实例。因此,您可以通过提供https:localhost/whatever/path/you/want的回调url来处理oauth2重定向url。只要确保在oau

这主要是因为缺乏对oauth2的理解,可能并不是electron特有的,但是我想了解一下有人会如何处理来自桌面平台的oauth2重定向url,比如electron


假设应用程序中没有webservice设置,桌面应用程序将如何提示用户提供针对第三方oauth2服务的凭据,然后正确验证这些凭据?

Electron JS在本地主机上运行浏览器实例。因此,您可以通过提供https:localhost/whatever/path/you/want的回调url来处理oauth2重定向url。只要确保在oauth2应用程序注册页面上为您正在使用的任何服务列出它

例如:

var authWindow = new BrowserWindow({
    width: 800, 
    height: 600, 
    show: false, 
    'node-integration': false,
    'web-security': false
});
// This is just an example url - follow the guide for whatever service you are using
var authUrl = 'https://SOMEAPI.com/authorize?{client_secret}....'

authWindow.loadURL(authUrl);
authWindow.show();
// 'will-navigate' is an event emitted when the window.location changes
// newUrl should contain the tokens you need
authWindow.webContents.on('will-navigate', function (event, newUrl) {
    console.log(newUrl);
    // More complex code to handle tokens goes here
});

authWindow.on('closed', function() {
    authWindow = null;
});

从本页中获得了很多灵感:

感谢您提供此解决方案。我还注意到,当没有单击浏览器窗口触发重定向到应用程序重定向uri时,WebContent中的导航事件是不可靠的。例如,如果我已经在浏览器窗口中登录,Github登录页面将永远不会使用重定向URI触发此事件。(它可能使用了一些会话存储)

我找到的解决方法是使用


我正在使用Electron进行Spotify的身份验证。为此,我需要在
authUrl
中传递重定向URI。如何使用
https://localhost/callback
当不使用Node.js的“express”模块来定义路径时?感谢这个解决方案,它非常有用。一个注意事项-我发现对于某些API,“did get redirect request”事件比“will navigate”事件更可靠,如果您已经获得授权,它们会立即重定向您。您能澄清一下“electron js在您的本地主机上运行浏览器实例”是什么意思吗?当electron app打包用于生产时,情况并非如此。正如您所看到的,这是我从KeyClope得到的错误-请注意,重定向uri不再是有效的uri,而是引用index.html文件。。警告[org.keydape.events](默认任务-4)类型=登录错误,realmId=codingpedia,clientId=invoices用户界面,userId=null,ipAddress=127.0.0.1,错误=无效重定向uri,重定向\u uri=file:///tmp/.mount_react-myxJe9/resources/app.asar/build/index.html@johnwick0831您最终是如何解决问题的?它会阻止重定向并显示空白页面,但现在如何将用户发送到index.html之类的特定页面,因此整个流程在回调函数中有意义,您可以传递重定向URL。这里提供了API文档:这确实帮助了我。我的应用程序已经处理了所有oAuth工作客户端,因此其他示例没有用处。一个建议可能是在设置url后添加这一行const query=details.url.split(“?”)[1];mainWindow.loadURL('file://${\uu dirname}/./index.html?${query})```
const { session } = require('electron');

// my application redirect uri
const redirectUri = 'http://localhost/oauth/redirect'

// Prepare to filter only the callbacks for my redirectUri
const filter = {
  urls: [redirectUri + '*']
};

// intercept all the requests for that includes my redirect uri
session.defaultSession.webRequest.onBeforeRequest(filter, function (details, callback) {
  const url = details.url;
  // process the callback url and get any param you need

  // don't forget to let the request proceed
  callback({
    cancel: false
  });
});