【问题标题】:GITLAB API Create new file in repositoryGITLAB API 在存储库中创建新文件
【发布时间】:2021-09-17 14:36:03
【问题描述】:

所以我试图通过这个 api 在我的 git repo 中创建一个新文件,我在我的 react 代码库中公开了这个文件,我得到了 403 错误?让我知道我哪里错了。

var bodyData = {
      token: "**********",
      ref: "main",
      branch: "demo",
      commit_message: "create a new file",
      content: "some content"
    };
    fetch("https://gitlab.com/api/v4/projects/27622003/repository/files/myfile1%2Etxt", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(bodyData),
    })
      .then((response) => response.json())
      .then((data) => {
        console.log("response:", data);
      })
      .catch((error) => {
        console.error("Error:", error);
      });

【问题讨论】:

  • 我不熟悉 gitlab,所以这不是答案,但是根据我的经验,错误 403 意味着您提供了正确的令牌,但是您的 API 无权调用 Gitlab。我假设您需要执行一个步骤来允许您的 API 进行调用

标签: reactjs gitlab gitlab-api


【解决方案1】:

您似乎缺少包含您想要执行的操作类型(创建、移动、更新和删除)的操作数组。

GitLab 提交 API:https://docs.gitlab.com/ee/api/commits.html

在您的情况下,有效负载应如下所示:

var bodyData = {
  token: "**********",
  ref: "main",
  branch: "demo",
  commit_message: "create a new file",
  actions: [
      {
         "action": "create",
         "file_path": "your/file/path",
         "content": "some content"
      }
  ]
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 2013-11-04
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    相关资源
    最近更新 更多