从电子浏览器窗口获取json



我正在尝试使用discords的Oauth2为我的应用程序创建登录,目前我正在为API调用显示单独的BrowserWindow,因为discords Oauth2要求用户单击授权。我的API调用返回acess_tokens的原始JSON。在我的应用程序的当前状态下,单独的窗口只显示JSON。我需要一种方法来从窗口内或从变量中的请求中获取JSON。我似乎找不到任何方法来访问原始内容。

function createAuthWindow(){
var authWindow = new BrowserWindow({
width: 400, 
height: 600, 
show: false, 
'node-integration': false,
'web-security': false,
icon: getFile('f','/src/asset/instance.png'),
});
// This is just an example url - follow the guide for whatever service you are using
var authUrl = 'http://localhost:3001/api/discord/login'

authWindow.loadURL(authUrl, (res) => {
console.log(res)
console.log(authWindow);
});
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) {
// More complex code to handle tokens goes here
console.log(event.code);
authWindow.webContents.session.webRequest.onCompleted({ urls: [newUrl] }, (details) => {
// Access request headers via details.requestHeaders
// Access response headers via details.responseHeaders
console.log(authWindow.webContents.code)
});
});

听起来你的身份验证URL不正确,你应该发送一条授权代码流消息,这样你就可以将令牌返回到你的应用程序。

桌面应用程序的常用技术是:

  • 格式化授权重定向URL
  • 在系统浏览器中打开此URL,系统浏览器将为您处理重定向
  • 通过专用URI方案或环回通知接收响应
  • 将授权码交换为令牌,然后您的应用程序可以使用令牌来调用API

重定向URL将是这样的值,尽管我没有使用Discord作为提供商,所以这可能不是100%正确的:

https://login.discord.com/oauth2/v2.0/authorize?
client_id=y792f434f
&response_type=code
&redirect_uri=com.mycompany.myapp:/callback
&scope=...
&state=...
&code_challenge=...
&code_challenge_method=S256

如果有帮助的话,我有几个关于电子桌面应用程序OAuth的博客文章。这是一个很难实现的流程。。。

  • 初始桌面代码示例
  • 最终桌面代码示例

最新更新