我正在尝试通过Admin REST API和Python创建Keycloak客户端。
我尝试了以下方法:
导入请求 导入argparse 导入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' } 返回ast.literal_eval(requests.post(url,params,verify = False).content.decode(“ utf-8” ))['access_token'] #返回request.post(url,params,verify = False).content.decode('utf-8') def create_client(): url ='http:// localhost:9003 / auth / admin / realms / master / clients' 标头= { 'content-type':'application / json', 'Authorization':'bearer'+ str(get_token) } params = { “ clientId”:“ testclient-3”, #“ id”:“ 3”, #“ name”:“ testclient-3”, #“ description” :“ TESTCLIENT-3”, #“已启用”:True, #“ redirectUris”:[“ \\”], #“ publicClient”:True } 返回请求。post (URL,标头,参数,verify = False) x = create_client() 打印(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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句