通过curl命令在Keycloack上创建用户

时间:2018-09-21 09:21:06

标签: curl keycloak

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

这是我的curl命令:

#!/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

enter image description here

我有此错误: enter image description here

我的境界很好 enter image description here

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

2 个答案:

答案 0 :(得分:2)

尝试一下,我添加了内容类型标题并修改了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"}'

答案 1 :(得分:1)

根据Keycloak的文档(Server Admin > The Admin CLI > Basic operations and resource URIs),func startObserveNearbyPosts(location: CLLocation) { let geoFireQuery = geoFire?.query(at: location, withRadius: 100) geoFireQuery?.observe(.keyEntered, with: { (key, _) in print("Key entered") }) geoFireQuery?.observeReady { print("Observe ready") } } 端点应为:

users

因此,请相应地修改您的最后一个网址。

您还可以在Keycloak's JIRA issue #5383上找到完整的示例。请注意,它还将显式添加content-type标头:

http://localhost:8080/auth/admin/realms/apiv2/users