昔我往矣

使用gitlab的api管理用户

2015年03月21日

以下内容涉及到Linux的curl指令、htpp的GET、DELETE和POST方法。非管理员用户只能操作自己的账号,管理员用户权限较大,回车前,请反复检查命令参数。在以下命令中,使用Private token做用户认证,用户可以在http://yougitlab-website/profile/account 查到自己的token,演示中以your-private-token做替代。

从用户名获取用户id

api调用大多使用id来操作,但是人类能识别的只是用户名,所以第一步,从用户名获取id就显得尤为重要。search后面可以是username,也可以是email。解析返回的json,其中的id就是用户的id号。

$ curl  "git.xnow.com/api/v3/users?private_token=your-private-token&search=who"
$ curl  "git.xnow.com/api/v3/users?private_token=your-private-token&search=who@xnow.me"

查看用户信息

获取自己账号的用户信息,以下两条命令皆可:

$ GET "http://git.xnow.me/api/v3/user?private_token=your-private-token"
$ curl "http://git.xnow.me/api/v3/user?private_token=your-private-token"

获取指定uid用户的信息

$ curl "http://git.xnow.me/api/v3/users/uid?private_token=your-private-token"
$ GET "http://git.xnow.me/api/v3/users/uid?private_token=your-private-token"

获取所有用户的信息

$ GET "http://git.xnow.me/api/v3/users?private_token=your-private-token"
$ curl "http://git.xnow.me/api/v3/users?private_token=your-private-token"

以上查找命令注意users和user,以及是否有uid(把uid替换成真正的用户id)的区别,管理员和普通用户所能看到的内容也是有区别的,下面的增删操作只适用于管理员。

添加账号

增加一个gitlab账号,POST方法:

$ curl --header "PRIVATE-TOKEN: your-admin-private-token" \
--data "password=1234abcd&email=xm@xnow.me&username=xme&name=xme2" \
"http://git.xnow.me/api/v3/users"

注意,password要有一定的复杂度,否则会报40x的错误!

删除账号

删除一个gitlab账号,DELETE方法:

$ curl -X DELETE "http://git.xnow.me/api/v3/users/uid?private_token=your-admin-private-token"

参考:http://doc.gitlab.com/ce/api/users.html

当前暂无评论 »

添加新评论 »