Way of importing the user credentials to running EMQX

Hi,

I am writing to ask about a way to import user data (username and password) to running EMQX via API call.

I followed this tutorial: Use HTTP API to Manage User Data | EMQX Documentation but got no success.

The EMQX is running in ECS task with a load balancer.

My code (XXX is my EMQX URL):

import requests
from requests.auth import HTTPBasicAuth

url = ‘XXX/api/v5/authentication/password_based%3Abuilt_in_database/import_users’

Admin credentials for authentication

username = ‘khoatest’
password = ‘khoatest@123’

The user data you want to send in JSON format

user_data = [
{
“user_id”: “myuser1”,
“password_hash”: “c5e46903df45e5dc096dc74657610dbee8deaacae656df88a1788f1847390242”,
“salt”: “e378187547bf2d6f0545a3f441aa4d8a”,
“is_superuser”: True
},
{
“user_id”: “myuser2”,
“password_hash”: “f4d17f300b11e522fd33f497c11b126ef1ea5149c74d2220f9a16dc876d4567b”,
“salt”: “6d3f9bd5b54d94b98adbcfe10b6d181f”,
“is_superuser”: False
}
]

Set the appropriate headers for JSON data

headers = {‘Content-Type’: ‘multipart/form-data’}

Sending the POST request

response = requests.post(url, json=user_data, auth=HTTPBasicAuth(username, password), headers=headers)

Checking the response

if response.status_code == 200:
print(“Users successfully imported to EMQX.”)
else:
print(f"Failed to import users. Status code: {response.status_code}, Response: {response.text}")

I got error: Failed to import users. Status code: 401, Response: {“code”:“BAD_API_KEY_OR_SECRET”,“message”:“Check api_key/api_secret”}.

I have ensure:

  • It is a POST request
  • Content type: multipart/form-data
  • username and password is correct

If someone has got the same error, please help me. Any help would be greatly appreciated!

Hello,

All EMQX REST API calls must be authenticated with an auth token. For security reasons, basic HTTP auth with login and password can’t be used. The token can be obtained via /api/v5/login endpoint or the dashboard, e.g.

curl -X 'POST' \
  'http://localhost:18083/api/v5/login' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "username": "....",
  "password": "...."
}'

Then the token can be used as follows:

curl -X 'GET' \
  'http://localhost:18083/api/v5/...' \
  -H 'accept: application/json' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'