通过 REST 创建 Keycloak 客户端 API
Create Keycloak client via REST API
我正在尝试通过 Admin REST API 和 Python 创建一个 Keycloak 客户端。
我试过以下方法:
import requests
import argparse
import ast
def get_token():
url = 'http://localhost:9003/auth/realms/master/protocol/openid-connect/token'
params = {
'client_id': 'admin-cli',
'grant_type': 'password',
'username' : 'admin',
'password': 'password'
}
return ast.literal_eval(requests.post(url, params, verify=False).content.decode("utf-8"))['access_token']
#return requests.post(url, params, verify=False).content.decode('utf-8')
def create_client():
url ='http://localhost:9003/auth/admin/realms/master/clients'
headers = {
'content-type': 'application/json',
'Authorization' : 'bearer '+ str(get_token)
}
params = {
"clientId":"testclient-3",
#"id":"3",
#"name": "testclient-3",
#"description": "TESTCLIENT-3",
#"enabled": True,
#"redirectUris":[ "\" ],
#"publicClient": True
}
return requests.post(url, headers, params, verify=False)
x = create_client()
print (x)
我收到 401 HTTP 代码,有人可以帮我解决这个问题吗?
提前致谢。
PS: 9003 映射到 8080(我使用的是 Keycloak docker 容器)
我已经做到了,是这样的:
import requests
import argparse
import ast
def get_token():
url = 'http://localhost:9003/auth/realms/master/protocol/openid-connect/token'
params = {
'client_id': 'admin-cli',
'grant_type': 'password',
'username' : 'admin',
'password': 'password'
}
x = requests.post(url, params, verify=False).content.decode('utf-8')
print (x)
print ('\n')
return ast.literal_eval(x)['access_token']
#return requests.post(url, params, verify=False).content.decode('utf-8')
def create_client():
url ='http://localhost:9003/auth/admin/realms/master/clients'
headers = {
'content-type': 'application/json',
'Authorization' : 'Bearer '+ str(get_token())
}
params = {
"clientId" : "testclient",
"id":"3",
"name": "testclient-3",
"description": "TESTCLIENT-3",
"enabled": True,
"redirectUris":[ "\" ],
"publicClient": True
}
x = requests.post(url, headers=headers, json=params)
print (x)
return x.content
我正在尝试通过 Admin REST API 和 Python 创建一个 Keycloak 客户端。
我试过以下方法:
import requests import argparse import ast def get_token(): url = 'http://localhost:9003/auth/realms/master/protocol/openid-connect/token' params = { 'client_id': 'admin-cli', 'grant_type': 'password', 'username' : 'admin', 'password': 'password' } return ast.literal_eval(requests.post(url, params, verify=False).content.decode("utf-8"))['access_token'] #return requests.post(url, params, verify=False).content.decode('utf-8') def create_client(): url ='http://localhost:9003/auth/admin/realms/master/clients' headers = { 'content-type': 'application/json', 'Authorization' : 'bearer '+ str(get_token) } params = { "clientId":"testclient-3", #"id":"3", #"name": "testclient-3", #"description": "TESTCLIENT-3", #"enabled": True, #"redirectUris":[ "\" ], #"publicClient": True } return requests.post(url, headers, params, verify=False) x = create_client() print (x)
我收到 401 HTTP 代码,有人可以帮我解决这个问题吗?
提前致谢。
PS: 9003 映射到 8080(我使用的是 Keycloak docker 容器)
我已经做到了,是这样的:
import requests
import argparse
import ast
def get_token():
url = 'http://localhost:9003/auth/realms/master/protocol/openid-connect/token'
params = {
'client_id': 'admin-cli',
'grant_type': 'password',
'username' : 'admin',
'password': 'password'
}
x = requests.post(url, params, verify=False).content.decode('utf-8')
print (x)
print ('\n')
return ast.literal_eval(x)['access_token']
#return requests.post(url, params, verify=False).content.decode('utf-8')
def create_client():
url ='http://localhost:9003/auth/admin/realms/master/clients'
headers = {
'content-type': 'application/json',
'Authorization' : 'Bearer '+ str(get_token())
}
params = {
"clientId" : "testclient",
"id":"3",
"name": "testclient-3",
"description": "TESTCLIENT-3",
"enabled": True,
"redirectUris":[ "\" ],
"publicClient": True
}
x = requests.post(url, headers=headers, json=params)
print (x)
return x.content