Github 个人访问 token ,用于访问私有(private)仓库的 :/repos/:user/:repo/contents

标签 github github-api github-api-v3 github-api-v4

我正在尝试使用 个人访问 token 用于访问 内容 的一个存储库。

如果存储库是公开的,我可以使用 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
        }
      }
    }
  }
}

现在我已经生成了一个个人访问 token ,用于在我的一个私有(private)存储库中执行此操作,但它从不返回任何内容:

v3(带有授权 token ):
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(带有授权 token ):
query {
  repository(owner: "myusername", name: "myrepo") {
    object(expression: "master:") {
      ... on Tree {
        entries{
          name
        }
      }
    }
  }
}

结果:
{
  "data": {
    "repository": {
      "object": null
    }
  }
}

我试过检查所有 read生成 token 时的复选框,但什么也没有。我究竟做错了什么?

最佳答案

看起来,要获取这些信息,都是repo需要 token 的访问权限。

对我有用:

 [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"
    }
  },
...
]

关于Github 个人访问 token ,用于访问私有(private)仓库的 :/repos/:user/:repo/contents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58716876/

相关文章:

git - 使用 github API,创建 git pull 请求而不检查代码

github - 使用 GitHub REST APIv3 进行全局代码搜索

powershell - 为 github 存储库中的环境创建审阅者

github-api-v3 - “Validation failed” 安装GitHub App 搜索私有(private)仓库报错

git - 提交后向 Github 用户名显示头像

github - 无法在 github 上创建存储库

与内容 API 等效的 GitHub GraphQL

python - 向 urllib2.Request 添加数据

git - 如何安全地更改github账户名?

android - 无法运行从 Github 导入的项目