技術(shù)簡(jiǎn)介
GitHub API是一個(gè)功能強(qiáng)大的工具,為開發(fā)者提供了訪問(wèn)和操作GitHub平臺(tái)上資源的途徑。無(wú)論是構(gòu)建個(gè)人工具,集成自動(dòng)化流程,還是開發(fā)應(yīng)用程序,GitHub API都提供了廣泛的功能。本文將介紹如何使用GitHub API,以及一些常見的用例。
GitHub API是基于RESTful風(fēng)格的API,允許開發(fā)者通過(guò)HTTP請(qǐng)求訪問(wèn)GitHub上的資源。這些資源包括倉(cāng)庫(kù)(Repositories)、用戶(Users)、問(wèn)題(Issues)、分支(Branches)等。通過(guò)GitHub API,你可以實(shí)現(xiàn)從查看存儲(chǔ)庫(kù)信息到管理問(wèn)題和合并請(qǐng)求等各種操作。
官方文檔:
申請(qǐng)token
獲取訪問(wèn)令牌:
要開始使用GitHub API,首先需要?jiǎng)?chuàng)建一個(gè)GitHub帳戶,并生成一個(gè)訪問(wèn)令牌(Access Token)。訪問(wèn)令牌允許你進(jìn)行身份驗(yàn)證并訪問(wèn)你有權(quán)訪問(wèn)的資源。在GitHub上,你可以在"Settings" -> “Developer settings” -> "Personal access tokens"中生成令牌。
簡(jiǎn)單使用
使用 curl 發(fā)送請(qǐng)求:
使用curl是最簡(jiǎn)單的方式來(lái)測(cè)試GitHub API。以下是一個(gè)獲取用戶信息的例子:
curl -H "Authorization: token YOUR_ACCESS_TOKEN" https://api.github.com/user
使用Apifox調(diào)用測(cè)試api
參考文檔:https://apifox.com/apiskills/how-to-use-github-api/
使用Java調(diào)用
@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);
}
獲取GitHub topic
寫一個(gè)Spring Boot單元測(cè)試
@SpringBootTest
public class GitHubTest {
@Test
public void test() {
try {
//設(shè)置感興趣的主題
String topic = "SpringBoot";
//定義api路徑地址
String url = "https://api.github.com/search/repositories?q=topic:" + topic;
//創(chuàng)建請(qǐng)求對(duì)象
// 創(chuàng)建HttpClient對(duì)象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 聲明訪問(wèn)地址
HttpGet httpGet = new HttpGet(url);
// 設(shè)置請(qǐng)求頭
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");
// 發(fā)起請(qǐng)求
CloseableHttpResponse response = httpClient.execute(httpGet);
// 判斷狀態(tài)碼是否是200
if (response.getStatusLine().getStatusCode() == 200) {
// 解析數(shù)據(jù)
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
-
@SpringBootTest
:這是一個(gè)Spring Boot測(cè)試注解,表示這是一個(gè)基于Spring Boot的測(cè)試類。 -
@Test
:這是JUnit測(cè)試框架的注解,用于標(biāo)識(shí)測(cè)試方法。 -
String topic = "SpringBoot";
:定義了感興趣的主題,這里是"SpringBoot"。 -
String url = "https://api.github.com/search/repositories?q=topic:" + topic;
:構(gòu)建GitHub API的搜索URL,通過(guò)指定主題進(jìn)行搜索。 -
CloseableHttpClient httpClient = HttpClients.createDefault();
:創(chuàng)建一個(gè)默認(rèn)的CloseableHttpClient
對(duì)象,用于發(fā)送HTTP請(qǐng)求。 -
HttpGet httpGet = new HttpGet(url);
:創(chuàng)建一個(gè)HTTP GET請(qǐng)求對(duì)象,指定GitHub API的搜索URL。 - 設(shè)置請(qǐng)求頭:
-
"User-Agent"
:用于標(biāo)識(shí)請(qǐng)求的用戶代理,模擬瀏覽器訪問(wèn)。 -
"Authorization"
:使用訪問(wèn)令牌進(jìn)行身份驗(yàn)證。請(qǐng)注意,代碼中的"Athorization"
應(yīng)該是"Authorization"
的拼寫錯(cuò)誤。 -
"Accept"
:指定接受的響應(yīng)類型為GitHub的JSON格式。 -
"X-GitHub-Api-Version"
:指定GitHub API的版本。
-
-
CloseableHttpResponse response = httpClient.execute(httpGet);
:發(fā)起HTTP GET請(qǐng)求,獲取響應(yīng)對(duì)象。 - 判斷響應(yīng)狀態(tài)碼是否為200:如果響應(yīng)狀態(tài)碼為200,將響應(yīng)實(shí)體解析為字符串,并打印輸出。
返回?cái)?shù)據(jù)實(shí)示例:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-804943.html
{
"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項(xiàng)目是一套電商系統(tǒng),包括前臺(tái)商城系統(tǒng)及后臺(tái)管理系統(tǒng),基于SpringBoot+MyBatis實(shí)現(xiàn),采用Docker容器化部署。 前臺(tái)商城系統(tǒng)包含首頁(yè)門戶、商品推薦、商品搜索、商品展示、購(gòu)物車、訂單流程、會(huì)員中心、客戶服務(wù)、幫助中心等模塊。 后臺(tái)管理系統(tǒng)包含商品管理、訂單管理、會(huì)員管理、促銷管理、運(yùn)營(yíng)管理、內(nèi)容管理、統(tǒng)計(jì)報(bào)表、財(cái)務(wù)管理、權(quán)限管理、設(shè)置等模塊。",
"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
},
總結(jié)
GitHub API提供了豐富的功能,允許開發(fā)者構(gòu)建強(qiáng)大的工具和應(yīng)用程序。通過(guò)了解如何獲取訪問(wèn)令牌,發(fā)送請(qǐng)求,以及一些常見用例,你可以更好地利用GitHub API來(lái)支持你的項(xiàng)目和工作流程。希望本文能夠幫助你更好地理解和使用GitHub API。在下一篇文章中,我會(huì)以如何在GitHub上進(jìn)行代碼搜索(查重)來(lái)介紹GitHub API的進(jìn)階使用。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-804943.html
到了這里,關(guān)于GitHub API使用--獲取GitHub topic的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!