这篇文章介绍一下在Gitlab中通过API创建用户时取消邮箱验证的方法。
现象说明使用API创建用户,登录时显示类似如下信息: You have to confirm your email address before continuing.
原因与对应方法因为出现的原因是由于用户的邮箱验证,所以一般可以使用两种方式:
- 完成当前用户有限验证:在gitlab的邮箱设定OK,而且用户邮箱也正确并能收到邮件,并且网络没有特殊访问设定限制的情况下:通过设定邮箱中的连接,点击打开完成验证,再次登录即可。
- 创建用户时取消邮箱验证:在创建用户是指定是否要进行邮箱验证即可。
本文memo的内容为Gitlab的Api为v4版本的情况,v3的对应方式略有不同
环境准备 docker-compose文件liumiaocn:gitlab liumiao$ cat docker-compose.yml version: '2' services: # Version Control service: Gitlab gitlab: image: gitlab/gitlab-ce:12.10.5-ce.0 ports: - "32001:80" volumes: - ./log/:/var/log/gitlab - ./data/:/var/opt/gitlab - ./conf/:/etc/gitlab restart: "no" liumiaocn:gitlab liumiao$事前准备
创建如下目录:
liumiaocn:gitlab liumiao$ ls docker-compose.yml liumiaocn:gitlab liumiao$ mkdir -p log data conf liumiaocn:gitlab liumiao$ ls conf data docker-compose.yml log liumiaocn:gitlab liumiao$启动
启动命令:docker-compose up -d
登录并创建apitoken 登录URL- http://localhost:32001
注:此处设定root用户密码,由于后续直接使用token进行用户创建,示例说明中不再直接需要使用root设定的密码。
通过settings菜单或者直接使用如下URL,创建api用的token
- http://localhost:32001/profile/personal_access_tokens
执行命令 access_token=“7F2jdsYyeDsuhGnyTvPz” gitlab_url=“localhost:32001” curl -X GET -H "PRIVATE-TOKEN: a c c e s s t o k e n " h t t p : / / {access_token}" http:// accesstoken"http://{gitlab_url}/api/v4/users
执行日志示例:
liumiaocn:gitlab liumiao$ access_token="7F2jdsYyeDsuhGnyTvPz" liumiaocn:gitlab liumiao$ gitlab_url="localhost:32001" liumiaocn:gitlab liumiao$ curl -X GET -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users |jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 808 100 808 0 0 7279 0 --:--:-- --:--:-- --:--:-- 7279 [ { "id": 1, "name": "Administrator", "username": "root", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", "web_url": "http://638d10b0f106/root", "created_at": "2020-07-19T02:20:24.191Z", "bio": null, "location": null, "public_email": "", "skype": "", "linkedin": "", "twitter": "", "website_url": "", "organization": null, "job_title": "", "work_information": null, "last_sign_in_at": "2020-07-19T02:24:26.401Z", "confirmed_at": "2020-07-19T02:20:23.698Z", "last_activity_on": "2020-07-19", "email": "admin@example.com", "theme_id": 1, "color_scheme_id": 1, "projects_limit": 100000, "current_sign_in_at": "2020-07-19T02:24:26.401Z", "identities": [], "can_create_group": true, "can_create_project": true, "two_factor_enabled": false, "external": false, "private_profile": false, "is_admin": true } ] liumiaocn:gitlab liumiao$创建用户
使用如下示例命令创建用户
curl -X POST -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users \ -H 'cache-control: no-cache' \ -H 'content-type: application/json' \ -d '{ "email": "test@outlook.com", "username": "liumiaocn", "password": "12341234", "name": "liumiao" }'
执行日志示例
liumiaocn:gitlab liumiao$ curl -X POST -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users \ > -H 'cache-control: no-cache' \ > -H 'content-type: application/json' \ > -d '{ "email": "test@outlook.com", > "username": "liumiaocn", > "password": "12341234", > "name": "liumiao" > }' |jq . % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 841 100 736 100 105 1790 255 --:--:-- --:--:-- --:--:-- 2041 { "id": 2, "name": "liumiao", "username": "liumiaocn", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/e1128bc3a06f4735b039c9a60ba7c445?s=80&d=identicon", "web_url": "http://638d10b0f106/liumiaocn", "created_at": "2020-07-19T02:33:42.010Z", "bio": null, "location": null, "public_email": "", "skype": "", "linkedin": "", "twitter": "", "website_url": "", "organization": null, "job_title": "", "work_information": null, "last_sign_in_at": null, "confirmed_at": null, "last_activity_on": null, "email": "test@outlook.com", "theme_id": 1, "color_scheme_id": 1, "projects_limit": 100000, "current_sign_in_at": null, "identities": [], "can_create_group": true, "can_create_project": true, "two_factor_enabled": false, "external": false, "private_profile": false, "is_admin": false } liumiaocn:gitlab liumiao$登录确认
v4的情况下,通过设定skip_confirmation即可对取消邮箱验证进行设定,示例命令如下所示:
curl -X POST -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users \ -H 'cache-control: no-cache' \ -H 'content-type: application/json' \ -d '{ "email": "liumiaocn@outlook.com", "username": "liumiao", "password": "12341234", "name": "liumiao", "skip_confirmation": "true" }'
执行示例日志
liumiaocn:gitlab liumiao$ curl -X POST -H "PRIVATE-TOKEN: ${access_token}" http://${gitlab_url}/api/v4/users \ > -H 'cache-control: no-cache' \ > -H 'content-type: application/json' \ > -d '{ "email": "liumiaocn@outlook.com", > "username": "liumiao", > "password": "12341234", > "name": "liumiao", > "skip_confirmation": "true" > }' {"id":3,"name":"liumiao","username":"liumiao","state":"active","avatar_url":"https://www.gravatar.com/avatar/95c1f7ff72d71b448592a335ba80fb64?s=80\u0026d=identicon","web_url":"http://638d10b0f106/liumiao","created_at":"2020-07-19T02:40:24.899Z","bio":null,"location":null,"public_email":"","skype":"","linkedin":"","twitter":"","website_url":"","organization":null,"job_title":"","work_information":null,"last_sign_in_at":null,"confirmed_at":"2020-07-19T02:40:24.857Z","last_activity_on":null,"email":"liumiaocn@outlook.com","theme_id":1,"color_scheme_id":1,"projects_limit":100000,"current_sign_in_at":null,"identities":[],"can_create_group":true,"can_create_project":true,"two_factor_enabled":false,"external":false,"private_profile":false,"is_admin":false}liumiaocn:gitlab liumiao$结果确认
输入用户名/密码进行登录
可以看到已经可以正常登录了
注意api的版本为v3的时候,此设定选项为confirm。