Github个人访问令牌用于访问私有存储库:/repos/:user/:repo/contents

4

我正在尝试使用个人访问令牌来访问存储库的内容

如果存储库是公共的,我可以使用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”复选框,但还是没有成功。我做错了什么?
1个回答

5

看起来,获取这些信息需要令牌具有所有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"
    }
  },
...
]

谢谢。它也适用于 v4。奇怪的是,为了获取文件列表,必须请求完整的仓库控制权。 - Marinos An

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接