github - 用于访问私有存储库的 Github 个人访问令牌:/repos/:user/:repo/contents
问题描述
我正在尝试使用个人访问令牌来访问存储库的内容。
如果存储库是公共的,我可以使用 v3 和 v4 api 来实现这一点。以下两个请求都返回内容:
v3:
curl https://api.github.com/repos/w3c/webappsec/contents/
v4:
query {
repository(owner: "w3c", name: "webappsec") {
object(expression: "master:") {
... on Tree {
entries{
name
}
}
}
}
}
现在我已经生成了一个个人访问令牌,用于在我的一个私有存储库中执行此操作,但它从不返回任何内容:
v3(带有授权令牌):
curl -H "Authorization: bearer myauthorizationtoken" https://api.github.com/repos/myusername/myrepo/contents/
结果:
{
"message": "Not Found",
"documentation_url": "https://developer.github.com/v3/repos/contents/#get-contents"
}
v4(带有授权令牌):
query {
repository(owner: "myusername", name: "myrepo") {
object(expression: "master:") {
... on Tree {
entries{
name
}
}
}
}
}
结果:
{
"data": {
"repository": {
"object": null
}
}
}
我尝试read
在生成令牌时检查所有复选框,但没有。我究竟做错了什么?
解决方案
看起来,要获取此信息,repo
需要令牌的所有访问权限。
为我工作:
[x] repo Full control of private repositories
[x] repo:status Access commit status
[x] repo_deployment Access deployment status
[x] public_repo Access public repositories
[x] repo:invite Access repository invitations
权限复选框
API v3 用法:
$ curl -H "Authorization: bearer $private_token" https://api.github.com/repos/dmytrohoi/site.source/contents/
[
{
"name": ".github",
"path": ".github",
"sha": "hash",
"size": 0,
"url": "https://api.github.com/repos/dmytrohoi/site.source/contents/.github?ref=master",
"html_url": "https://github.com/dmytrohoi/site.source/tree/master/.github",
"git_url": "https://api.github.com/repos/dmytrohoi/site.source/git/trees/hash",
"download_url": null,
"type": "dir",
"_links": {
"self": "https://api.github.com/repos/dmytrohoi/site.source/contents/.github?ref=master",
"git": "https://api.github.com/repos/dmytrohoi/site.source/git/trees/hash",
"html": "https://github.com/dmytrohoi/site.source/tree/master/.github"
}
},
...
]
推荐阅读
- angular - 在 Angular 模块中延迟加载 JS
- javascript - 为什么条件语句中的增量运算符会产生类 Object 的编译错误?
- typescript - 带有可选项目的固定数组长度
- stm32 - STM32 上 FreeRTOS 编译的 Truestudio 编译错误
- javascript - Firebase:如何重新验证自定义经过身份验证的用户?
- php - 在关联数组中搜索,然后删除某些值相同的元素?
- angular - 使用 document.forms.input.value 设置其值后具有未定义值的输入字段
- json - wso2 JMS JSON 到没有肥皂信封或非 xml html 的 html 电子邮件
- react-native - 无法在已设置状态的未安装组件上调用 setState(或 forceUpdate)
- html - 在 chrome 上测试响应性时按钮无法正确显示