【问题标题】:get json from electron BrowserWindow从电子浏览器窗口获取 json
【发布时间】:2021-03-24 04:19:44
【问题描述】:

我正在尝试使用 discord 的 Oauth2 为我的应用程序创建登录名,目前我正在为 API 调用显示一个单独的 BrowserWindow,因为 discords Oauth2 要求用户单击授权。我的 API 调用返回 access_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)
          });
    });

【问题讨论】:

    标签: node.js oauth-2.0 electron discord


    【解决方案1】:

    听起来您的身份验证 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
    

    如果有帮助的话,我有几篇关于 Electron 桌面应用程序的 OAuth 的博文。虽然这是一个棘手的流程......

    【讨论】:

    • 所以目前我有一个带有/login 路由的快速服务器登录路由将用户发送到这样的网址https://discord.com/api/oauth2/authorize?client_id=${CLIENT_ID}&redirect_uri=${redirect}&response_type=code&scope=identify 之后它转到/callback 路由这是我得到的它还获取https://discordapp.com/api/oauth2/token 的代码,这为我提供了我需要的所有信息,但从我的快递服务器以 json 格式提供
    猜你喜欢
    • 2018-05-20
    • 2011-02-18
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    相关资源
    最近更新 更多