GitHub Api用户和仓库常用接口

准备工作

官方文档:https://docs.github.com/en/rest/reference

1、申请github api token

2、在请求header中添加Authorization和Accept,例如:

在这里插入图片描述

用户

用户集合

请求说明

显示所有github用户信息,默认按id递增。

NameTypeInDescription
acceptstringheaderSetting to application/vnd.github.v3+json is recommended.
sinceintegerqueryA user ID. Only return users with an ID greater than this ID.
per_pageintegerqueryResults per page (max 100)Default: 30

请求url:https://api.github.com/users?since=100

返回响应

![(https://img-blog.csdnimg.cn/4a8aad1b81db49068620a7f5ef2426ce.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAQVVaUQ==,size_20,color_FFFFFF,t_70,g_se,x_16)

获取某个用户信息

请求说明
NameTypeInDescription
acceptstringheaderSetting to application/vnd.github.v3+json is recommended.
usernamestringpath

请求url:https://api.github.com/users/mojombo

返回响应

在这里插入图片描述

查看用户的关注/被关注

请求说明
https://api.github.com/users/USERNAME/following

请求url:https://api.github.com/users/mojombo/following

返回响应

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QQWSFV09-1633916354680)(C:\Users\Dell\AppData\Roaming\Typora\typora-user-images\image-20211008111711159.png)]

仓库

仓库集合

请求说明

查看所有仓库

NameTypeInDescription
acceptstringheaderSetting to application/vnd.github.v3+json is recommended.
sinceintegerqueryA repository ID. Only return repositories with an ID greater than this ID.

请求url:https://api.github.com/repositories?since=824

返回响应[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jlwdv9Km-1633916354682)(C:\Users\Dell\AppData\Roaming\Typora\typora-user-images\image-20211008163636812.png)]

列举仓库信息

请求说明

显示某组织所有的仓库信息

https://api.github.com/orgs/ORG/repos
NameTypeInDescription
acceptstringheaderSetting to application/vnd.github.v3+json is recommended.
orgstringpath
typestringquerySpecifies the types of repositories you want returned. Can be one of all, public, private, forks, sources, member, internal. Note: For GitHub AE, can be one of all, private, forks, sources, member, internal. Default: all. If your organization is associated with an enterprise account using GitHub Enterprise Cloud or GitHub Enterprise Server 2.20+, type can also be internal. However, the internal value is not yet supported when a GitHub App calls this API with an installation access token.
sortstringqueryCan be one of created, updated, pushed, full_name.Default: created
directionstringqueryCan be one of asc or desc. Default: when using full_name: asc, otherwise desc
per_pageintegerqueryResults per page (max 100)Default: 30
pageintegerqueryPage number of the results to fetch.Default: 1

请求url:https://api.github.com/orgs/nasa/repos?per_page=10

返回响应

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LPYsDrJN-1633916354684)(C:\Users\Dell\AppData\Roaming\Typora\typora-user-images\image-20211008113248445.png)]

仓库贡献人员

请求说明

查看某个仓库的贡献者名单

/repos/{owner}/{repo}/contributors

请求url:https://api.github.com/repos/nasa/osal/contributors

返回响应

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hlVoq1Tr-1633916354685)(C:\Users\Dell\AppData\Roaming\Typora\typora-user-images\image-20211008154924553.png)]

仓库语言

请求说明

查看仓库中不同编程语言的使用量,单位为字节。

/repos/{owner}/{repo}/languages

请求url:https://api.github.com/repos/nasa/osal/languages

返回响应

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KPxY8twr-1633916354686)(C:\Users\Dell\AppData\Roaming\Typora\typora-user-images\image-20211008155330250.png)]

仓库标签

请求说明

查看仓库中的所有标签

/repos/{owner}/{repo}/tags
NameTypeInDescription
acceptstringheaderSetting to application/vnd.github.v3+json is recommended.
ownerstringpath
repostringpath
per_pageintegerqueryResults per page (max 100)Default: 30
pageintegerqueryPage number of the results to fetch.Default: 1

请求url:https://api.github.com/repos/nasa/osal/tags

返回响应

在这里插入图片描述

仓库话题

请求说明

查看仓库中的所有话题

/repos/{owner}/{repo}/topics

请求url:https://api.github.com/repos/nasa/scrub/topics

返回响应

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aA19fNS7-1633916354689)(C:\Users\Dell\AppData\Roaming\Typora\typora-user-images\image-20211008162955430.png)]

查看用户仓库

请求说明

查看某用户的所有仓库信息

/users/{username}/repos
NameTypeInDescription
acceptstringheaderSetting to application/vnd.github.v3+json is recommended.
usernamestringpath
typestringqueryCan be one of all, owner, member.Default: owner
sortstringqueryCan be one of created, updated, pushed, full_name.Default: full_name
directionstringqueryCan be one of asc or desc. Default: asc when using full_name, otherwise desc
per_pageintegerqueryResults per page (max 100)Default: 30
pageintegerqueryPage number of the results to fetch.Default: 1

请求url:https://api.github.com/users/JustinGOSSES/repos

返回响应

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UQmgYKUU-1633916354690)(C:\Users\Dell\AppData\Roaming\Typora\typora-user-images\image-20211008164014330.png)]

分支

请求说明

查看仓库的分支

/repos/{owner}/{repo}/branches
NameTypeInDescription
acceptstringheaderSetting to application/vnd.github.v3+json is recommended.
ownerstringpath
repostringpath
protectedbooleanquerySetting to true returns only protected branches. When set to false, only unprotected branches are returned. Omitting this parameter returns all branches.
per_pageintegerqueryResults per page (max 100)Default: 30
pageintegerqueryPage number of the results to fetch.Default: 1

请求url:https://api.github.com/repos/nasa/osal/branches

返回响应
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sYhus1cp-1633916354691)(C:\Users\Dell\AppData\Roaming\Typora\typora-user-images\image-20211008165838831.png)]
  • 11
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: Dart语言支持调用GitHub API接口,可以使用GitHub的REST API来实现。要实现给GitHub上的项目打星,可以使用starEndpoint API来实现,具体的步骤如下: 1. 使用GitHub的OAuth2.0授权机制,获取访问令牌。 2. 使用令牌,调用starEndpoint API,传入需要给项目打星的项目信息。 3. 根据API返回的响应,检查是否给项目成功打星。 ### 回答2: 使用Dart调用GitHub APIGitHub上的项目打星可以通过以下步骤实现: 1. 首先,您需要在您的Dart项目中导入http包,使用它来发送HTTP请求并与GitHub API进行通信。 ```dart import 'package:http/http.dart' as http; ``` 2. 接下来,您需要设置GitHub API的请求头,以便进行身份验证。在GitHub上生成一个访问令牌,并将其作为请求头的Authorization字段。 ```dart String baseUrl = "https://api.github.com"; String token = "YOUR_GITHUB_ACCESS_TOKEN"; Map<String, String> headers = { "Accept": "application/vnd.github.v3+json", "Authorization": "token $token", }; ``` 3. 调用GitHub API中的“给存储库打星”接口,向存储库发送一个PUT请求。在请求中,您需要提供存储库的所有者名称和存储库名称。 ```dart String owner = "OWNER_NAME"; String repository = "REPOSITORY_NAME"; String url = "$baseUrl/user/starred/$owner/$repository"; http.Response response = await http.put(url, headers: headers); if (response.statusCode == 204) { print("成功为存储库打星!"); } else { print("无法为存储库打星:${response.reasonPhrase}"); } ``` 在上面的代码示例中,我们使用了await关键字,这意味着我们需要将代码放在一个异步函数内部。这样可以确保我们在等待从GitHub API获得的响应时,不会阻塞Dart的执行。 请注意,您需要将“OWNER_NAME”和“REPOSITORY_NAME”替换为实际的存储库所有者和存储库名称。 通过以上步骤,您可以使用Dart调用GitHub API实现给GitHub上的项目打星功能。 ### 回答3: 在使用Dart实现调用GitHub APIGitHub上的项目打星时,可以按照以下步骤进行: 第一步,确保已经在自己的Dart项目中引入了http包,可通过在`pubspec.yaml`文件中添加`http: ^0.12.0`依赖并运行`pub get`命令来安装。 第二步,导入http包和dart:convert包: ```dart import 'package:http/http.dart' as http; import 'dart:convert'; ``` 第三步,构建函数来调用GitHub API,为特定的GitHub项目打星: ```dart Future<void> starGitHubProject(String owner, String repo, String accessToken) async { final url = 'https://api.github.com/user/starred/$owner/$repo'; final response = await http.put( Uri.parse(url), headers: { 'Authorization': 'token $accessToken', 'Content-Length': '0', }, ); if(response.statusCode == 204){ print('成功给项目打星'); } else{ print('给项目打星失败:${response.statusCode}'); } } ``` 上述函数接收三个参数:`owner`表示GitHub项目的拥有者,`repo`表示GitHub项目的仓库名称,以及`accessToken`表示用户的访问令牌,用于进行身份认证。 最后,我们可以调用上述函数来给GitHub上的项目打星,例如: ```dart void main() async { final owner = '拥有者用户名'; final repo = '项目仓库名称'; final accessToken = '访问令牌'; await starGitHubProject(owner, repo, accessToken); } ``` 以上是使用Dart通过调用GitHub APIGitHub上的项目打星的简单示例代码。请注意,需要替换示例代码中的`owner`、`repo`和`accessToken`为自己合适的值才能正常运行。同时,为确保用户的访问安全,更推荐将访问令牌保存在安全的地方,而不直接硬编码在代码中。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值