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

zan*_*ane 5 octokit.net github-api-v3 github-app

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

\n
https://api.github.com/search/code?q=abc+in:file+repo:username/private-repo\n
Run Code Online (Sandbox Code Playgroud)\n

但是,这会返回以下响应。

\n
{\n    "message": "Validation Failed",\n    "errors": [\n        {\n            "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.",\n            "resource": "Search",\n            "field": "q",\n            "code": "invalid"\n        }\n    ],\n    "documentation_url": "https://docs.github.com/v3/search/"\n}\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

那么为什么搜索会失败呢?

\n

zan*_*ane 2

向 GitHub 支持提出了请求。他们的回应:

查询失败,因为 GitHub App 无权访问 read私有仓库的内容。元数据read 权限将允许您搜索存储库,但没有足够的范围来read存储存储库的内容(私有)。

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