Python 3.x 通过RESTAPI创建KeyClope客户端

Python 3.x 通过RESTAPI创建KeyClope客户端,python-3.x,keycloak,keycloak-services,Python 3.x,Keycloak,Keycloak Services,我正试图用Python通过ADMINRESTAPI创建一个KeyClope客户端 我尝试了以下方法: 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',

我正试图用Python通过ADMINRESTAPI创建一个KeyClope客户端

我尝试了以下方法:

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) 导入请求 导入argparse 导入ast def get_令牌(): url='1〕http://localhost:9003/auth/realms/master/protocol/openid-连接/令牌' 参数={ '客户端id':'管理cli', “授权类型”:“密码”, “用户名”:“管理员”, “密码”:“密码” } 返回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='1〕http://localhost:9003/auth/admin/realms/master/clients' 标题={ “内容类型”:“应用程序/json”, “授权”:“持有人”+str(获取令牌) } 参数={ “客户端ID”:“testclient-3”, #“id”:“3”, #“名称”:“testclient-3”, #“说明”:“TESTCLIENT-3”, #“启用”:正确, #“重定向URI”:[“\\”], #“公共客户”:正确 } return requests.post(url、标题、参数、verify=False) x=创建_客户端() 打印(x) 我得到401HTTP代码,有人能帮我吗

先谢谢你


PS:9003被映射到8080(我正在使用KeyClope 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