通过curl命令在Keycloack上创建用户 [英] Create user on Keycloack from curl command

查看:326
本文介绍了通过curl命令在Keycloack上创建用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我尝试通过Keycloak的Admin REST API从curl命令创建用户. 我可以以管理员身份进行身份验证,我的回答很好,但是当我要创建用户时,会出现类似"404-Not Found"的错误.

Currently I try to create a user from curl command via Keycloak's Admin REST API. I can authenticate myself as an admin, I have a good answer, but when I want to create a user, I have an error like: "404 - Not Found".

这是我的curl命令:

Here are my curl commands:

#!/bin/bash

echo "* Request for authorization"
RESULT=`curl --data "username=pierre&password=pierre&grant_type=password&client_id=admin-cli" http://localhost:8080/auth/realms/master/protocol/openid-connect/token`

echo "\n"
echo "* Recovery of the token"
TOKEN=`echo $RESULT | sed 's/.*access_token":"//g' | sed 's/".*//g'`

echo "\n"
echo "* Display token"
echo $TOKEN

echo "\n"
echo " * user creation\n"
curl   http://localhost:8080/apiv2/users -H "Authorization: bearer $TOKEN"   --data '{"firstName":"xyz","lastName":"xyz", "email":"demo2@gmail.com", "enabled":"true"}'

我使用了位于以下地址的官方API文档: https://www.keycloak.org/docs-api/4.4/rest-api/index.html

I used the official API documentation, located at this address: https://www.keycloak.org/docs-api/4.4/rest-api/index.html

我有此错误:

我的境界很好

我该如何解决? 预先感谢.

How I can fix it? Thanks in advance.

推荐答案

尝试一下,我添加了内容类型标头并修改了url:

try this, I added the content type header and modify the url :

#!/bin/bash

echo "* Request for authorization"
RESULT=`curl --data "username=admin&password=Pa55w0rd&grant_type=password&client_id=admin-cli" http://localhost:8080/auth/realms/master/protocol/openid-connect/token`

echo "\n"
echo "* Recovery of the token"
TOKEN=`echo $RESULT | sed 's/.*access_token":"//g' | sed 's/".*//g'`

echo "\n"
echo "* Display token"
echo $TOKEN

echo "\n"
echo " * user creation\n"
curl -v http://localhost:8080/auth/admin/realms/apiv2/users -H "Content-Type: application/json" -H "Authorization: bearer $TOKEN"   --data '{"firstName":"xyz","lastName":"xyz", "email":"demo2@gmail.com", "enabled":"true"}'

这篇关于通过curl命令在Keycloack上创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆