Github API (flutter调用)

在用Flutter完成一款Github客户端后,对Github API的调用做一个小总结。
项目地址:https://github.com/MrHGJ/FlutterGithub

1. 常用接口

  • (获取用户信息) https://api.github.com/users/$username [用户名]
  • (获取用户repos列表)https://api.github.com/users/$username/repos [用户名]
  • (repos详细信息) https://api.github.com/repos/$username/$reposname [用户名,仓库名]
  • (repos下路径内容)https://api.github.com/repos/$repoOwner/$repoName/contents/$path
  • (readme文件内容)https://api.github.com/repos/$username/$reposname/readme
  • (获取当前仓库所有分支) https://api.github.com/repos/$repoOwner/$repoName/branches
  • (获取repos的commit列表)https://api.github.com/repos/$repoOwner/$repoName/commits
  • (获取某一次commit详情)https://api.github.com/repos/$repoOwner/$repoName/commits/$sha
  • (获取repos的动态)https://api.github.com/repos/$repoOwner/$repoName/events
  • (获取star该repos的用户)https://api.github.com/repos/$reposOwner/$reposName/stargazers
  • (获取用户follow的人)https://api.github.com/users/$userName/followers
  • (获取follow用户的人)https://api.github.com/users/$userName/following
  • (搜索接口)https://api.github.com/search/$type?q=$searchWords&sort=$sort&order=$order
///type: repositories(搜索仓库),users(搜索用户)
///searchWords: 搜索的关键词
///sort: 排序的方式,例如Best Match, Most Stars...
///order : desc(降序), asc(升序)
  • (用户是否star当前项目)https://api.github.com/user/starred/$repoOwner/$repoName【get】
  • (对当前项目进行star或者unStar)https://api.github.com/user/starred/$repoOwner/$repoName【PUT : DELETE】
  • (用户是否follow当前的developer)https://api.github.com/user/following/$developerName
  • …(待扩充)

2. 权限认证

大多数查询的API可以直接访问,例如获取用户信息、获取repos列表等,但一定时间内,访问接口超过60次后,github服务就会有限制,接口返回内容如下:

 "message": "API rate limit exceeded for 43.224.245.180. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)"

这时候就需要权限认证了,认证后API限制时间内访问次数可达5000次,可参考:https://developer.github.com/v3/#rate-limiting。另外,有些涉及到读写操作的Github API必须经过权限认证后才能正常调用。
根据官方文档介绍,权限认证有三种方式,可参考:
官方文档:https://developer.github.com/v3/(有问题多看看官方文档)
参考文档:https://segmentfault.com/a/1190000015144126

#3. OAuth2 key/secret 认证方式
在FlutterGithub中,采用了OAuth2 key/secret认证方式,具体步骤如下:

1. 申请client_id和client_secret。

申请地址:https://github.com/settings/applications/new。信息可以随便填写,没有什么具体要求。例如下图所示,填完后提交即可得到client_id和client_secret;
image.png

2. 获取token

通过如下方式可以获取token:

String basic = 'Basic ' + base64.encode(utf8.encode('$username:$pwd'));
final Map oAuthParams = {
  "client_id": 'xxxxxxxxxxxx',
  "client_secret": 'xxxxxxxxxxxxxxxxxxx'
};
_options.method = "post";
_options.headers["Authorization"] = basic;
var url = "https://api.github.com/authorizations";
var response = await dio.request(url,
    data: json.encode(oAuthParams), options: _options);
String token = response.data['token'];

3. 进行认证

在访问某个API时,此时认证的方法有两种:
一、将token放在头部进行访问(推荐),例如:

var url ='https://api.github.com/users/$username';
_options.method = "get";
_options.headers["Authorization"] = 'token $token';   //等价于'token ' + token
var response = await dio.request(url, options: _options);

二、 每次发起请求时,将client_id和client_secret作为参数带上。

var url = 'https://api.github.com/users/$username?client_id=YOUR-CLIENT-ID&client_secret=YOUR-CLIENT-SECRET';
var response = await dio.request(url);

4. 项目介绍

项目地址:用flutter实现的一款界面精美的Github App
介绍:用Flutter实现的一款界面精美、功能较全、体验良好的Github客户端。支持多语言、换肤等功能。代码简单易懂且有充分的注释,很适用于学习Flutter。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值