GitHub API: 从验证为安装的模板创建存储库

GitHub API: create a repository from template authenticated as installation

我正在尝试通过 GitHub 的 API 为组织从存储库模板创建存储库。 https://api.github.com/repos/{template_org_name}/{template_repo}/generate。我正在尝试使用经过身份验证的 GitHub 应用程序来执行此操作,该应用程序已作为对组织具有管理权限的安装进行身份验证。我无法成功创建存储库并获得 Resource not accessible by integration 响应。但是,我能够使用我自己的 GitHub 用户的个人访问令牌通过同一端点创建存储库。我倾向于认为此端点仅可作为 user-to-server 请求使用,但在查看文档时运气不佳 (https://developer.github.com/v3/repos/#create-repository-using-a-repository-template)。我知道它在技术上是一个测试端点,所以也许这就是我的答案。

我检查过我在请求中也使用了正确的 "Accept" header (Accept: application/vnd.github.baptiste-preview+json)。有人对这个端点有好运吗?

事实证明,从模板创建存储库的端点仅可作为 user-to-server 请求使用,并且未为 GitHub 应用程序启用。这是非常微妙的,但只有 API 文档中名称旁边带有信息图标的操作可用于 GitHub 应用程序。 Example of information icon

我也为此苦苦挣扎。事实证明它只能作为 user-to-server 请求使用,但可以使用 Github 应用程序完成,如下所述:https://docs.github.com/en/developers/apps/identifying-and-authorizing-users-for-github-apps

高级别:

  1. Select 在您的应用设置中“在安装期间请求用户授权 (OAuth)”,并在您的应用中添加回调 URL
  2. 从重定向(安装时)获取代码,如下所示:https://xxx.ngrok.io/?code=xxx&installation_id=xxx&setup_action=install
  3. 在 octokit.rb 中的 POST https://github.com/login/oauth/access_tokenexchange_code_for_token 处将代码交换为常规访问令牌。这需要:重定向中的 client_idclient_secretcode
  4. 使用返回的访问代码以用户身份直接访问 API。即 github = Octokit::Client.new(access_token: "xxx")
  5. 为用户创建一个存储库 github.create_repository('xxx', private: true)

另一个让我感到困惑的地方是 app_idclient_id 之间的区别。两者都列在您的应用程序页面上,但您需要 client_id!