GitHub API是一个功能强大的工具,为开发者提供了访问和操作GitHub平台上资源的途径。无论是构建个人工具,集成自动化流程,还是开发应用程序,GitHub API都提供了广泛的功能。本文将介绍如何使用GitHub API,以及一些常见的用例。
GitHub API是基于RESTful风格的API,允许开发者通过HTTP请求访问GitHub上的资源。这些资源包括仓库(Repositories)、用户(Users)、问题(Issues)、分支(Branches)等。通过GitHub API,你可以实现从查看存储库信息到管理问题和合并请求等各种操作。
官方文档:
获取访问令牌:
要开始使用GitHub API,首先需要创建一个GitHub帐户,并生成一个访问令牌(Access Token)。访问令牌允许你进行身份验证并访问你有权访问的资源。在GitHub上,你可以在"Settings" -> “Developer settings” -> "Personal access tokens"中生成令牌。
使用 curl 发送请求:
使用curl是最简单的方式来测试GitHub API。以下是一个获取用户信息的例子:
curl -H "Authorization: token YOUR_ACCESS_TOKEN" https://api.github.com/user
使用Apifox调用测试api
參考文档:https://apifox.com/apiskills/how-to-use-github-api/
@Test
void test() throws IOException {
HttpRequest request = HttpRequest.get("https://api.github.com/user")
.header("Accept", "application/vnd.github+json")
.header("Authorization", "Bearer <token>")
.header("X-GitHub-Api-Version", "2022-11-28");
HttpResponse response = request.execute();
System.out.println(response);
}
写一个Spring Boot单元测试
@SpringBootTest
public class GitHubTest {
@Test
public void test() {
try {
//设置感兴趣的主题
String topic = "SpringBoot";
//定义api路径地址
String url = "https://api.github.com/search/repositories?q=topic:" + topic;
//创建请求对象
// 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 声明访问地址
HttpGet httpGet = new HttpGet(url);
// 设置请求头
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.101.76 Safari/537.36");
httpGet.addHeader("Athorization", "Bearer <token>");
httpGet.addHeader("Accept", "application/vnd.github+json");
httpGet.addHeader("X-GitHub-Api-Version", "2022-11-28");
// 发起请求
CloseableHttpResponse response = httpClient.execute(httpGet);
// 判断状态码是否是200
if (response.getStatusLine().getStatusCode() == 200) {
// 解析数据
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@SpringBootTest
:这是一个Spring Boot测试注解,表示这是一个基于Spring Boot的测试类。@Test
:这是JUnit测试框架的注解,用于标识测试方法。String topic = "SpringBoot";
:定义了感兴趣的主题,这里是"SpringBoot"。String url = "https://api.github.com/search/repositories?q=topic:" + topic;
:构建GitHub API的搜索URL,通过指定主题进行搜索。CloseableHttpClient httpClient = HttpClients.createDefault();
:创建一个默认的CloseableHttpClient
对象,用于发送HTTP请求。HttpGet httpGet = new HttpGet(url);
:创建一个HTTP GET请求对象,指定GitHub API的搜索URL。"User-Agent"
:用于标识请求的用户代理,模拟浏览器访问。"Authorization"
:使用访问令牌进行身份验证。请注意,代码中的 "Athorization"
应该是 "Authorization"
的拼写错误。"Accept"
:指定接受的响应类型为GitHub的JSON格式。"X-GitHub-Api-Version"
:指定GitHub API的版本。CloseableHttpResponse response = httpClient.execute(httpGet);
:发起HTTP GET请求,获取响应对象。返回数据实示例:
{
"total_count": 11872,
"incomplete_results": false,
"items": [
{
"id": 127988011,
"node_id": "MDEwOlJlcG9zaXRvcnkxMjc5ODgwMTE=",
"name": "mall",
"full_name": "macrozheng/mall",
"private": false,
"owner": {
"login": "macrozheng",
"id": 15903809,
"node_id": "MDQ6VXNlcjE1OTAzODA5",
"avatar_url": "https://avatars.githubusercontent.com/u/15903809?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/macrozheng",
"html_url": "https://github.com/macrozheng",
"followers_url": "https://api.github.com/users/macrozheng/followers",
"following_url": "https://api.github.com/users/macrozheng/following{/other_user}",
"gists_url": "https://api.github.com/users/macrozheng/gists{/gist_id}",
"starred_url": "https://api.github.com/users/macrozheng/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/macrozheng/subscriptions",
"organizations_url": "https://api.github.com/users/macrozheng/orgs",
"repos_url": "https://api.github.com/users/macrozheng/repos",
"events_url": "https://api.github.com/users/macrozheng/events{/privacy}",
"received_events_url": "https://api.github.com/users/macrozheng/received_events",
"type": "User",
"site_admin": false
},
"html_url": "https://github.com/macrozheng/mall",
"description": "mall项目是一套电商系统,包括前台商城系统及后台管理系统,基于SpringBoot+MyBatis实现,采用Docker容器化部署。 前台商城系统包含首页门户、商品推荐、商品搜索、商品展示、购物车、订单流程、会员中心、客户服务、帮助中心等模块。 后台管理系统包含商品管理、订单管理、会员管理、促销管理、运营管理、内容管理、统计报表、财务管理、权限管理、设置等模块。",
"fork": false,
"url": "https://api.github.com/repos/macrozheng/mall",
"forks_url": "https://api.github.com/repos/macrozheng/mall/forks",
"keys_url": "https://api.github.com/repos/macrozheng/mall/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/macrozheng/mall/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/macrozheng/mall/teams",
"hooks_url": "https://api.github.com/repos/macrozheng/mall/hooks",
"issue_events_url": "https://api.github.com/repos/macrozheng/mall/issues/events{/number}",
"events_url": "https://api.github.com/repos/macrozheng/mall/events",
"assignees_url": "https://api.github.com/repos/macrozheng/mall/assignees{/user}",
"branches_url": "https://api.github.com/repos/macrozheng/mall/branches{/branch}",
"tags_url": "https://api.github.com/repos/macrozheng/mall/tags",
"blobs_url": "https://api.github.com/repos/macrozheng/mall/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/macrozheng/mall/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/macrozheng/mall/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/macrozheng/mall/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/macrozheng/mall/statuses/{sha}",
"languages_url": "https://api.github.com/repos/macrozheng/mall/languages",
"stargazers_url": "https://api.github.com/repos/macrozheng/mall/stargazers",
"contributors_url": "https://api.github.com/repos/macrozheng/mall/contributors",
"subscribers_url": "https://api.github.com/repos/macrozheng/mall/subscribers",
"subscription_url": "https://api.github.com/repos/macrozheng/mall/subscription",
"commits_url": "https://api.github.com/repos/macrozheng/mall/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/macrozheng/mall/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/macrozheng/mall/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/macrozheng/mall/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/macrozheng/mall/contents/{+path}",
"compare_url": "https://api.github.com/repos/macrozheng/mall/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/macrozheng/mall/merges",
"archive_url": "https://api.github.com/repos/macrozheng/mall/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/macrozheng/mall/downloads",
"issues_url": "https://api.github.com/repos/macrozheng/mall/issues{/number}",
"pulls_url": "https://api.github.com/repos/macrozheng/mall/pulls{/number}",
"milestones_url": "https://api.github.com/repos/macrozheng/mall/milestones{/number}",
"notifications_url": "https://api.github.com/repos/macrozheng/mall/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/macrozheng/mall/labels{/name}",
"releases_url": "https://api.github.com/repos/macrozheng/mall/releases{/id}",
"deployments_url": "https://api.github.com/repos/macrozheng/mall/deployments",
"created_at": "2018-04-04T01:11:44Z",
"updated_at": "2024-01-14T11:37:16Z",
"pushed_at": "2024-01-11T06:54:53Z",
"git_url": "git://github.com/macrozheng/mall.git",
"ssh_url": "git@github.com:macrozheng/mall.git",
"clone_url": "https://github.com/macrozheng/mall.git",
"svn_url": "https://github.com/macrozheng/mall",
"homepage": "https://www.macrozheng.com/admin/",
"size": 58454,
"stargazers_count": 73150,
"watchers_count": 73150,
"language": "Java",
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"has_discussions": false,
"forks_count": 28051,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 36,
"license": {
"key": "apache-2.0",
"name": "Apache License 2.0",
"spdx_id": "Apache-2.0",
"url": "https://api.github.com/licenses/apache-2.0",
"node_id": "MDc6TGljZW5zZTI="
},
"allow_forking": true,
"is_template": false,
"web_commit_signoff_required": false,
"topics": [
"docker",
"elasticsearch",
"elk",
"java",
"mongodb",
"mybatis",
"mysql",
"rabbitmq",
"redis",
"spring",
"spring-boot",
"spring-cloud",
"spring-security",
"springboot",
"springcloud",
"swagger-ui"
],
"visibility": "public",
"forks": 28051,
"open_issues": 36,
"watchers": 73150,
"default_branch": "master",
"score": 1.0
},
GitHub API提供了丰富的功能,允许开发者构建强大的工具和应用程序。通过了解如何获取访问令牌,发送请求,以及一些常见用例,你可以更好地利用GitHub API来支持你的项目和工作流程。希望本文能够帮助你更好地理解和使用GitHub API。在下一篇文章中,我会以如何在GitHub上进行代码搜索(查重)来介绍GitHub API的进阶使用。