21

我正在熟悉 GitHub API http://developer.github.com/v3/我正在尝试使用 Firefox 的 RESTClient 插件和 curl 命令行工具。

我已经找到了如何使用 API 创建 repo,但是我似乎无法使用 API 删除它。

根据这里的帮助:http: //developer.github.com/v3/repos/#delete-a-repository我必须发送这样的删除请求:

curl -X DELETE -H 'Authorization: token xxx' https://api.github.com/repos/:owner/:repo

帮助没有具体说明,我不确定 :owner 和 :repo 的含义 - 这些是名称还是 ID,但我尝试了几种组合的名称和 ID,但均未成功。我收到的回复是:

404 Not Found

我错过了什么?

4

2 回答 2

27

如果您通过应用程序页面创建了您正在使用的令牌,那么此令牌将具有以下范围user、、、、。您可以通过使用该令牌发出 API 请求并查看响应 HTTP 标头来验证这一点:public_reporepogist

curl -v -H 'Authorization: token xxx' https://api.github.com

查找X-OAuth-Scopes将具有范围列表的响应标头:

X-OAuth-Scopes: user, public_repo, repo, gist

但是,要删除存储库,令牌需要具有delete_repo范围

因此,您需要一个与您所拥有的范围不同的令牌。您可以使用Authorizations API创建这样的令牌:

curl -v -u username -X POST https://api.github.com/authorizations -d '{"scopes":["delete_repo"], "note":"token with delete repo scope"}'

这将返回一个带有新令牌的 JSON 文档,您应该可以使用它来删除存储库:

{
  "id": XXXXX,
  "url": "https://api.github.com/authorizations/XXXXX",
  "app": {
    "name": "GitHub API",
    "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api",
    "client_id": "00000000000000000000"
  },
  "token": "XXXXXX",
  "note": "token with delete repo scope",
  "note_url": null,
  "created_at": "2013-10-11T20:34:49Z",
  "updated_at": "2013-10-11T20:34:49Z",
  "scopes": [
    "delete_repo"
  ]
}

当然,以这种方式创建令牌时,您可以请求多个范围,而不仅仅是delete_repo范围。

另外,作为旁注,当您没有正确授权时 API 返回 404 错误的原因是为了防止信息泄漏

于 2013-10-11T20:48:55.973 回答
20

To delete a GitHub repo:

curl \
  -X DELETE \
  -H "Accept: application/vnd.github.v3+json" \
  -H "Authorization: token ${token}" \
   https://api.github.com/repos/${username}/${reponame}

Define or replace ${token}, ${username}, and ${reponame}. The token must have access to the delete_repo scope.

于 2015-06-04T12:41:53.557 回答