在安装 GitHub 应用程序的私有存储库中搜索时出现“验证失败”错误

“Validation failed” error on searching in private repository with installation of GitHub App

我创建了一个 GitHub 应用程序并将其安装在我的帐户中,允许它访问我帐户中的私有存储库。 GitHub 应用程序具有读取元数据的权限。 然后,我按照此处的步骤生成了一个 JWT 并使用它来创建安装访问令牌。 我尝试使用此令牌在上述私有存储库中使用 GitHub 搜索 API 搜索关键字,如下所示:

https://api.github.com/search/code?q=abc+in:file+repo:username/private-repo

然而,这returns以下响应。

{
    "message": "Validation Failed",
    "errors": [
        {
            "message": "The listed users and repositories cannot be searched either because the resources do not exist or you do not have permission to view them.",
            "resource": "Search",
            "field": "q",
            "code": "invalid"
        }
    ],
    "documentation_url": "https://docs.github.com/v3/search/"
}

我尝试使用此访问令牌获取此 GitHub 应用程序安装的存储库,并在响应中成功返回私有存储库。我认为这意味着安装可以访问私有存储库并且令牌按预期工作。 API 使用:https://api.github.com/installation/repositories.

为什么搜索失败?

在 GitHub 支持下提出了问题。他们的回应:

The query failed because the GitHub App does not have permission to read the content of the private repository. The Metadata read permission will allow you to search for repositories but does not have sufficient scope to read the content of the repository(private).

docs 将搜索 API 列在元数据下,但它应该在内容权限下。向 GitHub 应用授予内容 read 权限解决了该问题。