用户创建API:‘active’属性设置为True但返回响应中的为False

大家好,

我正在使用 Python 通过 Discourse API 创建用户。用户创建请求返回了一个成功响应,但我遇到了一个问题:即使我在请求体中明确将其设置为 true,响应中的 active 属性始终为 false。confirmed 属性设置为 true,并且工作正常。

这是代码:

import csv
import requests
import json
from datetime import datetime

# Discourse API 详细信息
discourse_url = "https://forum.hobiguru.com/"  # 替换为您的 Discourse URL
api_key = "your-api-key"  # 替换为您的 Discourse API 密钥
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,  # 默认设置为 True
        "confirmed": True,  # 默认设置为 True
        "created_at": join_date_str
    }

    print(f"\n请求 URL: {url}")
    print(f"请求头: {json.dumps(headers, indent=2)}")
    print(f"请求体: {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"用户创建成功: {json.dumps(response_json, indent=2)}")
        else:
            print(f"用户创建失败。原因: {response_json.get('message')}")
            print(f"错误: {json.dumps(response_json.get('errors'), indent=2)}")
    else:
        print(f"请求失败: {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'}
请求 URL: https://forum.hobiguru.com/users.json
请求头:
{
  "Api-Key": "d9---------------e564d65d9b5a3",
  "Api-Username": "system",
  "Content-Type": "application/json"
}
请求体:
{
  "name": "Goran",
  "email": "'bla+blaaa@gmail.com",
  "password": "P@ssword!23",
  "username": "goran12",
  "active": true,
  "confirmed": true,
  "created_at": "2024-11-04T10:20:34Z"
}
响应:

完整响应: {
  "success": true,
  "active": false,
  "message": "Vaš račun je aktiviran i spreman za korištenje."
}
请求成功,用户已创建:
{'success': True, 'active': False, 'message': 'Vaš račun je aktiviran i spreman za korištenje.'}

问题:

  • 尽管在请求体中传递了 “active”: True,但响应中的 active 属性始终为 false,并且我在 Discourse 管理面板中查找用户时找不到任何用户。
  • API 返回 success: true,但用户似乎并未完全激活。

问题:

  1. 我的请求中是否有遗漏的内容可能导致 active 属性未正确设置?
  2. Discourse API 中是否有任何特定条件会覆盖 active 属性,即使它被明确设置为 true?

任何见解或建议都将不胜感激!

2 个赞

您可以尝试运行一次此脚本,这可能会对您有所帮助。给您:用于为您的 Discourse 论坛创建虚拟用户的脚本

此外,您的脚本似乎运行得相当好,但问题出在您 CSV 文件中使用的数据上。

我使用以下示例数据测试了您的代码:

Username,Name,Email,Password,Bio,Location,Confirmed,Active,Join Date
Paul,Paul Paul,paul@example.com,tough001!@,This is Paul,Australia,TRUE,TRUE,2023-12-30T14:00:00Z
radhika01,Radhika Singh,singhradhika@example.com,tough001!@,This is Radhika Tomar,India,TRUE,TRUE,2023-12-30T14:00:00Z

对我来说,它运行得非常顺利。有几点需要注意:使用至少 12 个字符长的密码,并确保它们足够强壮。

试试看,如果您成功了,请告诉我 :))

@MihirR

非常感谢您的回复和帮助!我真的很感激。

我想我找到了问题所在。问题出在我使用的电子邮件地址上——特别是,用于虚拟用户的带有加号的电子邮件(例如 Gmail 别名 whatever+something@gmail.comwhatever+something2@gmail.com)。当我使用不带加号的电子邮件时,一切正常,用户会像您的 CSV 示例一样被创建。但是,当使用带有加号的电子邮件时,API 似乎无法正确处理它。

在截图中,您可以看到 2 个 curl 调用:一个带有加号,一个不带加号的电子邮件…

奇怪的是,当我使用加号时,我没有收到错误。 我实际上收到了一个成功的响应,表明帐户已创建并已激活,但这并非事实。我直接在 Rails 控制台中进行了检查,并连接到数据库,但用户不在那里。用户也没有出现在 discourse 管理员中。

看起来 API 的响应存在问题——它报告用户创建和激活成功(抱歉,截图中的字符串是克罗地亚语),但用户实际上从未在系统中创建。这表明 API 在处理或报告用户创建时可能存在问题,尤其是在电子邮件中使用特殊字符(如加号)时。

再次感谢您的帮助,我想与您分享这一发现,以防万一有帮助!

1 个赞

您的实例上的 normalize_emails 站点设置是如何配置的?

3 个赞

看起来它实际上报告了激活未成功,假设 \"active\": false 的意思是这样。

响应中的“message”键显示“Your account is activated and ready for use.”,这是错误的。

1 个赞

正是如此,这是整个问题中的错误部分。参数显示为 active:false,而消息显示为“账户已激活并可供使用”。

您好,响应参数显示 active:false,而消息内容却是“账户已激活并可供使用”。

我还没检查,但我会检查的。这似乎是在这里宣布的:Enabling e-mail normalization by default


是的,看起来失败的原因是标志 normalize_email:true,它默认就是true。当我将其关闭后,用户就创建成功了……但是,API 没有考虑 normalize_emails 标志,并且即使用户从未被创建或激活,它仍然返回成功事件 :confused: 我应该在 discourse github 上报告这个问题吗?

2 个赞

或者打开一个 Bug rest-api 主题 :slight_smile:

1 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.