Create Keycloak client via REST API
Asked Answered
C

1

6

I'm trying to create a Keycloak client through Admin REST API with Python.

I've tried the following:

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)

I'm getting 401 HTTP Code, anyone can help me with this please ?

Thank you in advance.

PS: 9003 is mapped to 8080 (I'm using Keycloak docker container)

Conducive answered 29/10, 2019 at 9:25 Comment(0)
C
13

I've done it, this way:

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
Conducive answered 29/10, 2019 at 11:17 Comment(1)
Once you have the x (response variable), you can get the token like this. response_data = json.loads(x)['access_token'] . Of course you need to import the native json library import json. This is an alternative to astCulminate

© 2022 - 2024 — McMap. All rights reserved.