Hi all,
I’m using Python to create users via the Discourse API. The user creation request is returning a success response, but I’m encountering an issue where the active attribute in the response is always false, even though I explicitly set it to true in the payload. The confirmed attribute is set to true, and that works fine.
Here’s the code:
import csv
import requests
import json
from datetime import datetime
# Discourse API details
discourse_url = "https://forum.hobiguru.com/" # Replace with your Discourse URL
api_key = "your-api-key" # Replace with your Discourse API Key
api_username = "system" # Admin username
# Request headers
headers = {
"Api-Key": api_key,
"Api-Username": api_username,
"Content-Type": "application/json"
}
# Function to create a user
def create_user(username, name, email, password, bio, location, confirmed, active, join_date):
url = f"{discourse_url}/users.json"
join_date_str = join_date.strftime('%Y-%m-%dT%H:%M:%SZ')
payload = {
"name": name,
"email": email,
"password": password,
"username": username,
"active": True, # Set active to True by default
"confirmed": True, # Set confirmed to True by default
"created_at": join_date_str
}
print(f"\nRequest URL: {url}")
print(f"Request Headers: {json.dumps(headers, indent=2)}")
print(f"Request Payload: {json.dumps(payload, indent=2)}")
response = requests.post(url, json=payload, headers=headers)
if response.status_code == 200:
response_json = response.json()
if response_json.get("success"):
print(f"User creation successful: {json.dumps(response_json, indent=2)}")
else:
print(f"User creation failed. Reason: {response_json.get('message')}")
print(f"Errors: {json.dumps(response_json.get('errors'), indent=2)}")
else:
print(f"Request failed: {response.status_code}, {response.text}")
# Reading users from a CSV file
with open('input_users.csv', mode='r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
# Extract user details from the row
create_user(row['Username'], row['Name'], row['Email'], row['Password'], row['Bio'], row['Location'], row['Confirmed'] == 'TRUE', row['Active'] == 'TRUE', datetime.strptime(row['Join Date'], '%Y-%m-%dT%H:%M:%SZ'))
I get this printed:
Request to create a user
{'name': 'Goran', 'email': 'bla+blaaa@gmail.com', 'password': 'P@ssword!23', 'username': 'goran12', 'active': True, 'confirmed': True, 'created_at': '2024-11-04T10:20:34Z'}
Request URL: https://forum.hobiguru.com/users.json
Request Headers:
{
"Api-Key": "d9---------------e564d65d9b5a3",
"Api-Username": "system",
"Content-Type": "application/json"
}
Request Payload:
{
"name": "Goran",
"email": "'bla+blaaa@gmail.com",
"password": "P@ssword!23",
"username": "goran12",
"active": true,
"confirmed": true,
"created_at": "2024-11-04T10:20:34Z"
}
Response:
Full Response: {
"success": true,
"active": false,
"message": "Va\u0161 ra\u010dun je aktiviran i spreman za kori\u0161tenje."
}
Request was successful and user got created:
{'success': True, 'active': False, 'message': 'Vaš račun je aktiviran i spreman za korištenje.'}
Problem:
• Despite passing “active”: True in the payload, the active attribute in the response is always false and when I look for the users in the discourse admin panel I cannot find any
• The API returns success: true, but the user does not seem to be fully activated.
Questions:
-
Is there something I might be missing in my request that could cause the active attribute to not be set correctly?
-
Are there any specific conditions in Discourse’s API that would override the active attribute, even if it’s explicitly set to true?
Any insights or advice would be greatly appreciated!