获取请求到ElectronJS中的Github API(渲染器)

7eumitmz  于 2023-03-21  发布在  Electron
关注(0)|答案(1)|浏览(501)

嘿,我有以下设置:ElectronJS使用电子力和ReactJS以及Typescript。到目前为止一切都运行良好,但是我需要对公共github API端点执行一个简单的获取请求。
这是终点:https://api.github.com/repos/nzhul/tic-tac-toe-online/releases/latest?owner=OWNER&repo=REPO
你可以试着匿名打开它,看看它是否可以公开访问。
我使用下面的代码来执行请求(github文档中推荐使用):我正在尝试在应用的React部分执行此请求。如果在main.ts中执行此请求,同样的请求也可以工作。

const getRepoInfo = async () => {
    const octokit = new Octokit();

    const response = await octokit.request("GET /repos/nzhul/tic-tac-toe-online/releases/latest",
        {
            owner: "OWNER",
            repo: "REPO",
            headers: {
                "X-GitHub-Api-Version": "2022-11-28",
            },
        }
    );

    // eslint-disable-next-line no-debugger
    return response;
};

出现以下错误:

Refused to connect to 'https://api.github.com/repos/nzhul/tic-tac-toe-online/releases/latest?owner=OWNER&repo=REPO'
    because it violates the following Content Security Policy directive: "default-src 'self' 'unsafe-inline' data:".
    Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.

在发布这篇文章之前,我已经搜索了类似的问题,并发现有人建议将此设置放在forge.config.ts中:

devContentSecurityPolicy:
        "default-src * self blob: data: gap:; style-src * self 'unsafe-inline' blob: data: gap:; script-src * 'self' 'unsafe-eval' 'unsafe-inline' blob: data: gap:; object-src * 'self' blob: data: gap:; img-src * self 'unsafe-inline' blob: data: gap:; connect-src self * 'unsafe-inline' blob: data: gap:; frame-src * self blob: data: gap:;",

这改变了我收到的错误,但我仍然得到这个:

Access to fetch at 'https://api.github.com/repos/nzhul/tic-tac-toe-online/releases/latest?owner=OWNER&repo=REPO' from origin 'http://localhost:3000' has been blocked by CORS policy:
    Request header field x-github-api-version is not allowed by Access-Control-Allow-Headers in preflight response.

基本上我的问题是:如何在ElectronJS(电子伪造)环境中正确执行对公共github API的API请求?

lyr7nygr

lyr7nygr1#

好的,我设法修复了我的问题,没有使用他们的API(Octokit),而是做简单的获取请求。

const response = await fetch("https://api.github.com/repos/nzhul/tic-tac-toe-online/releases/latest",
    {
        headers: {
            Authorization: "token {Personal_Access_Token}",
        },
    }
);

const data = await response.json();
console.log(data);

基本上我不需要这个帖子了,所以如果有人从版主决定,随时删除它。否则可能会考虑离开它,以防有人有同样的问题。

相关问题