مرحباً بالجميع،
أنا أستخدم بايثون لإنشاء مستخدمين عبر واجهة برمجة تطبيقات Discourse. طلب إنشاء المستخدم يعيد استجابة ناجحة، ولكني أواجه مشكلة حيث تكون السمة active في الاستجابة دائمًا false، على الرغم من أنني قمت بتعيينها صراحةً على true في الحمولة. السمة confirmed تم تعيينها على true، وهذا يعمل بشكل جيد.
هذا هو الكود:
import csv
import requests
import json
from datetime import datetime
# تفاصيل واجهة برمجة تطبيقات Discourse
discourse_url = "https://forum.hobiguru.com/" # استبدل بعنوان URL الخاص بـ Discourse
api_key = "your-api-key" # استبدل بمفتاح واجهة برمجة تطبيقات Discourse الخاص بك
api_username = "system" # اسم مستخدم المسؤول
# رؤوس الطلب
headers = {
"Api-Key": api_key,
"Api-Username": api_username,
"Content-Type": "application/json"
}
# دالة لإنشاء مستخدم
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, # تعيين active إلى True افتراضيًا
"confirmed": True, # تعيين confirmed إلى True افتراضيًا
"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}")
# قراءة المستخدمين من ملف CSV
with open('input_users.csv', mode='r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
# استخراج تفاصيل المستخدم من الصف
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'))
أحصل على هذا مطبوعًا:
طلب لإنشاء مستخدم
{'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š račun je aktiviran i spreman za korištenje."
}
Request was successful and user got created:
{'success': True, 'active': False, 'message': 'Vaš račun je aktiviran i spreman za korištenje.'}
المشكلة:
- على الرغم من تمرير
"active": Trueفي الحمولة، فإن السمةactiveفي الاستجابة تكون دائمًاfalse، وعندما أبحث عن المستخدمين في لوحة تحكم مسؤولي Discourse، لا يمكنني العثور على أي منهم. - تعيد واجهة برمجة التطبيقات
success: true، ولكن يبدو أن المستخدم لم يتم تنشيطه بالكامل.
الأسئلة:
- هل هناك شيء قد أكون أغفله في طلبي قد يتسبب في عدم تعيين السمة
activeبشكل صحيح؟ - هل هناك أي شروط محددة في واجهة برمجة تطبيقات Discourse يمكن أن تتجاوز السمة
active، حتى لو تم تعيينها صراحةً علىtrue؟
أي رؤى أو نصائح ستكون محل تقدير كبير!

