如何使用API​​删除GitHub存储库

时间:2013-10-11 13:33:39

标签: github-api

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

我已经了解了如何使用API​​创建回购,但我似乎无法使用API​​删除它。

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

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

帮助未指定,我不确定它们的含义:所有者和:repo - 这些是名称还是ID,但我尝试了几种名称和ID,但没有成功。我收到的回复是:

404 Not Found

我错过了什么?

3 个答案:

答案 0 :(得分:21)

如果您通过Applications page创建了令牌,那么此令牌将包含以下scopesuserpublic_reporepogist。您可以通过使用该令牌发出API请求并查看响应HTTP标头来验证这一点:

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

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

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

但是,要删除存储库,请token needs to have the delete_repo scope

因此,您需要一个与您拥有的范围不同的令牌。您可以使用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错误的原因是prevent information leakage

答案 1 :(得分:8)

删除存储库

curl -X DELETE -H 'Authorization: token {access token goes here}' https://api.github.com/repos/{yourUsername}/{name of repo} 替换里面的花括号和文本。

显示标题

curl -I https://api.github.com -I表示仅提取HTTP标头。

创建存储库

curl -u yourUsername -X POST https://api.github.com/user/repos -d '{"name":"nameOfRepo"}'

答案 2 :(得分:-1)

这是一个使用Apache Http Components(Java)的完整工作示例:

// imports

public class PostAndDeleteDemo {

    static String user = "your@email.com";
    static String password = "yourPassword";
    static String userName = "yourUserName";
    static String token = "yourToken";

    static CloseableHttpClient client = HttpClientBuilder.create().build();
    static CloseableHttpResponse response;

    public static void main(String[] args) throws IOException {

        createRepo();
        deleteRepo();

    }

    private static void deleteRepo() throws IOException {
        HttpDelete request = new HttpDelete(
        "https://api.github.com/repos/"+ userName +"/dummyRepo");

        request.setHeader(HttpHeaders.AUTHORIZATION, "token " + token);

        response = client.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();

        assertEquals(statusCode, 204);
    }

    private static void createRepo() throws IOException {

        String auth = user + ":" + password;
        byte[] encodedAuth = Base64.encodeBase64(
                auth.getBytes(Charset.forName("ISO-8859-1")));
        String authHeader = "Basic " + new String(encodedAuth);

        String json = "{\"name\": \"dummyRepo\"}";

        HttpPost httpPost = new HttpPost("https://api.github.com/user/repos");
        httpPost.setEntity(new StringEntity(json));
        httpPost.setHeader("Content-type","application/json");
        httpPost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);


        response = client.execute(httpPost);
        int statusCode = response.getStatusLine().getStatusCode();

        assertEquals(statusCode, 201);
    }
}
相关问题