GitHub API使用--获取GitHub topic

目录标题

    • 技术简介
    • 申请token
    • 简单使用
    • 使用Java调用
    • 获取GitHub topic
    • 总结

技术简介

GitHub API是一个功能强大的工具,为开发者提供了访问和操作GitHub平台上资源的途径。无论是构建个人工具,集成自动化流程,还是开发应用程序,GitHub API都提供了广泛的功能。本文将介绍如何使用GitHub API,以及一些常见的用例。
GitHub API是基于RESTful风格的API,允许开发者通过HTTP请求访问GitHub上的资源。这些资源包括仓库(Repositories)、用户(Users)、问题(Issues)、分支(Branches)等。通过GitHub API,你可以实现从查看存储库信息到管理问题和合并请求等各种操作。

官方文档:

申请token

获取访问令牌:

要开始使用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/

在这里插入图片描述

使用Java调用

   @Testvoid 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

写一个Spring Boot单元测试

@SpringBootTest
public class GitHubTest {@Testpublic 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);// 判断状态码是否是200if (response.getStatusLine().getStatusCode() == 200) {// 解析数据String content = EntityUtils.toString(response.getEntity(), "UTF-8");System.out.println(content);}} catch (Exception e) {e.printStackTrace();}}}
  1. @SpringBootTest:这是一个Spring Boot测试注解,表示这是一个基于Spring Boot的测试类。
  2. @Test:这是JUnit测试框架的注解,用于标识测试方法。
  3. String topic = "SpringBoot";:定义了感兴趣的主题,这里是"SpringBoot"。
  4. String url = "https://api.github.com/search/repositories?q=topic:" + topic;:构建GitHub API的搜索URL,通过指定主题进行搜索。
  5. CloseableHttpClient httpClient = HttpClients.createDefault();:创建一个默认的CloseableHttpClient对象,用于发送HTTP请求。
  6. HttpGet httpGet = new HttpGet(url);:创建一个HTTP GET请求对象,指定GitHub API的搜索URL。
  7. 设置请求头:
    • "User-Agent":用于标识请求的用户代理,模拟浏览器访问。
    • "Authorization":使用访问令牌进行身份验证。请注意,代码中的 "Athorization" 应该是 "Authorization" 的拼写错误。
    • "Accept":指定接受的响应类型为GitHub的JSON格式。
    • "X-GitHub-Api-Version":指定GitHub API的版本。
  8. CloseableHttpResponse response = httpClient.execute(httpGet);:发起HTTP GET请求,获取响应对象。
  9. 判断响应状态码是否为200:如果响应状态码为200,将响应实体解析为字符串,并打印输出。

返回数据实示例:

{"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的进阶使用。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/410031.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【latex】参考文献排版前移,在最前面引用\usepackage{url}

【LaTeX】参考文献排版前移&#xff0c;在最前面引用\usepackage{url} 写在最前面完整解决步骤请教申申latex编译报错解决方案 写在最前面 参考文献从21开始排版前移了 解决方案&#xff1a;在最前面加一行 \usepackage{url}完整解决步骤 请教申申 申申yyds&#xff01;&am…

Windows NT 3.5源代码已编译!

2020年5月&#xff0c;Windows NT 3.5 build 782源代码被泄露。然而&#xff0c;它缺少很多文件&#xff0c;包括编译器、链接器、头文件等。大多数这些工具都可以从 Windows NT 3.5 的 SDK 和 DDK 中应用&#xff08;您也可以临时处理 NT 3.51 的 DDK 文件&#xff0c;但之后根…

1.环境部署

1.虚拟机安装redhat8系统 这个其实很简单&#xff0c;但是有一点小细节需要注意。 因为我的电脑是 16核心的&#xff0c;所以选择内核16&#xff0c;可以最大发挥虚拟机的性能 磁盘选择SATA&#xff0c;便于后期学习 将一些没用的设备移除 选择安装redhat 8 时间选择上海 选择…

使用 Python 创造你自己的计算机游戏(游戏编程快速上手)第四版:第十九章到第二十一章

十九、碰撞检测 原文&#xff1a;inventwithpython.com/invent4thed/chapter19.html 译者&#xff1a;飞龙 协议&#xff1a;CC BY-NC-SA 4.0 碰撞检测涉及确定屏幕上的两个物体何时相互接触&#xff08;即发生碰撞&#xff09;。碰撞检测对于游戏非常有用。例如&#xff0c;如…

string 模拟实现

string的数据结构 char* _str; size_t _size; size_t _capacity; _str 是用来存储字符串的数组&#xff0c;采用new在堆上开辟空间&#xff1b; _size 是用来表示字符串的长度&#xff0c;数组大小strlen(_str)&#xff1b; _capacity 是用来表示_str的空间大小, _capacity…

Androidmanifest文件加固和对抗

前言 恶意软件为了不让我们很容易反编译一个apk&#xff0c;会对androidmanifest文件进行魔改加固&#xff0c;本文探索androidmanifest加固的常见手法以及对抗方法。这里提供一个恶意样本的androidmanifest.xml文件&#xff0c;我们学完之后可以动手实践。 1、Androidmanife…

响应式Web开发项目教程(HTML5+CSS3+Bootstrap)第2版 第1章 HTML5+CSS3初体验 项目1-2 许愿墙

项目展示 在生活中&#xff0c;许愿墙是一种承载愿望的实体&#xff0c;来源于“许愿树”的习俗。后来人们逐渐改变观念&#xff0c;开始将愿望写在小纸片上&#xff0c;然后贴在墙上&#xff0c;这就是许愿墙。随着互联网的发展&#xff0c;人们又将许愿墙搬到了网络上&#…

C# 基础入门

第二章 C# 语法基础 2-1 C# 中的关键字 关键字&#xff0c;是一些被C#规定了用途的重要单词。 在Visual Studio的开发环境中&#xff0c;关键字被标识为蓝色&#xff0c;下图代码中&#xff0c;用红方框圈出的单词就是关键字。 关键字 class &#xff0c;这个关键字的用途是…

【C语言】数据结构——排序三(归并与计数排序)

&#x1f497;个人主页&#x1f497; ⭐个人专栏——数据结构学习⭐ &#x1f4ab;点击关注&#x1f929;一起学习C语言&#x1f4af;&#x1f4ab; 目录 导读&#xff1a;1. 归并排序1.1 基本思想1.2 递归实现1.3 非递归实现 2. 计数排序2.1 基本思想2.2 代码实现 导读&#x…

考研机试题收获——高精度进制转换

代码的第一遍真的很重要&#xff0c;在第一次打的时候尽量把问题思考全面&#xff0c;不要漏打少打&#xff0c;尽量不要留bug给之后de。 一、基础方面 一、处理输出的结束问题 scanf和cin默认都不会读取空格 ①scanf()&#xff1a;如果从文件中读取数据&#xff0c;当scanf()…

134基于matlab的时间序列预测

基于matlab的时间序列预测&#xff0c;包括最小二乘支持向量机和粒子群优化支持向量机及改进的粒子群优化支持向量机。输出测试结果&#xff0c;具有GUI可视化界面。程序已调通&#xff0c;可直接运行。 134matlab时间序列预测粒子群优化 (xiaohongshu.com)

.Net 8.0 Web API Controllers 添加到 windows 服务

示例源码下载&#xff1a;https://download.csdn.net/download/hefeng_aspnet/88747022 创建 Windows 服务的方法之一是从工作线程服务模板开始。 但是&#xff0c;如果您希望能够让它托管 API 控制器&#xff08;也许是为了查看它正在运行的进程的状态&#xff09;&#xff0…