首页 文章

如何使用API删除GitHub存储库

提问于
浏览
15

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

我已经了解了如何使用API创建repo,但是我似乎无法使用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 回答

  • 21

    这是使用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);
        }
    }
    
  • 7

    Delete Repository

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

    Display Header

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

    Create Repository

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

  • -1

    如果您通过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 .

相关问题