我想获得所有具有特定扩展名的GitHub文件的链接。我发现我可以使用extension:bin
搜索所有*.bin
文件,URLhttps://github.com/search?q=extension%3Abin&type=代码
我发现使用GitHub API检索具有特定扩展名的文件但这个答案涉及特定的存储库。
更新:我正在尝试https://api.github.com/search/code?q=extension:ifc+大小:1000..1500,但响应为
{
"message": "Validation Failed",
"errors": [
{
"message": "Must include at least one user, organization, or repository",
}
]
}
我们在GitHub API中有一个方法可以在所有存储库中搜索吗?我没在这里找到https://docs.github.com/en/rest/search#search-代码
根本原因分析
我通过在没有授权标头的情况下执行HTTP请求来重现该问题:
$ curl
--request GET
--header 'Accept: application/vnd.github+json'
'https://api.github.com/search/code?q=extension:ifc+size:1000..1500'
{
"message": "Validation Failed",
"errors": [
{
"message": "Must include at least one user, organization, or repository",
"resource": "Search",
"field": "q",
"code": "invalid"
}
],
"documentation_url": "https://docs.github.com/v3/search/"
}
根本原因
因此,您似乎错过了授权标头。
解决方案
使用适当的授权标头执行HTTP请求。
草拟示例命令行
curl
--request GET
--header 'Accept: application/vnd.github+json'
--header 'Authorization: Bearer <TOKEN>'
'https://api.github.com/search/code?q=extension:ifc+size:1000..1500'
示例命令行对我来说运行良好(返回搜索结果(
请将<TOKEN>
替换为GitHub个人访问令牌。
其他参考资料
- 搜索:搜索代码-GitHub文档
- 问题。如何使用GitHub API在GitHub中搜索代码?-堆栈溢出