API 正在抛出 404 错误

您好 Discourse 社区,

我目前在使用 Discourse API 端点时遇到了一些挑战,特别是与暂停用户相关的操作,非常希望能得到您的帮助或指导。

问题摘要

  1. 404 未找到

    • 在使用端点 /admin/users/15/suspend.json 时,即使用户存在,我也会一直收到“页面未找到”的错误。
    • 我也尝试了备用端点 /admin/u/15/suspend.json,但结果相同。
  2. 意外的 HTML 响应

    • API 返回的是一个 HTML 页面,显示消息“哎呀!该页面不存在或已设为私有。”,而不是预期的 JSON 响应。
    • 无论我使用 curl 还是 PowerShell 进行请求,都会出现此问题。
  3. API 密钥和权限

    • 使用的 API 密钥具有完整的管理员访问权限,但请求仍然返回 404 错误。
    • 我也尝试使用精细控制的密钥(仅限于暂停操作)和只读密钥,但问题依然存在。
    • 有趣的是,通过浏览器访问该端点时工作正常,但使用带有密钥的 API 请求时却失败了。

请求示例

curl 示例

curl -X PUT "https://your-discourse-domain.com/admin/users/15/suspend.json" \
-H "Api-Key: [REDACTED_API_KEY]" \
-H "Api-Username: system" \
-H "Content-Type: application/json" \
-d '{"suspend_until": "2024-12-31", "reason": "Violation of community guidelines"}'

PowerShell 示例

# PowerShell 脚本用于在 Discourse 上暂停用户

$baseUrl = "https://your-discourse-domain.com"
$userId = 15
$apiKey = "[REDACTED_API_KEY]"
$apiUsername = "system"
$suspendUntil = "2024-12-31"
$reason = "Violation of community guidelines"

$headers = @{
    "Api-Key" = $apiKey
    "Api-Username" = $apiUsername
    "Content-Type" = "application/json"
}

$body = @{
    suspend_until = $suspendUntil
    reason = $reason
} | ConvertTo-Json

$endpoint = "$baseUrl/admin/users/$userId/suspend.json"

try {
    $response = Invoke-RestMethod -Uri $endpoint -Method Put -Headers $headers -Body $body -ErrorAction Stop
    Write-Host "用户已成功暂停!" -ForegroundColor Green
} catch {
    Write-Host "暂停用户时出错:" -ForegroundColor Red
    Write-Host $_.Exception.Message
}

附加详情

  • Discourse 版本:3.4.0.beta3
  • 已采取的故障排除步骤
    我尝试调整请求头,包括 User-AgentReferer,但均无效。

非常感谢您抽出宝贵时间提供任何见解或建议来帮助解决这些问题!

此致,
Ross

您的示例在我们的测试站点上按原样工作,除了预期的参数:

○ → curl -i -X PUT "https://try.discourse.org/admin/users/41/suspend.json" \
  -H "Api-Key: swordfish" \
  -H "Api-Username: michael" \
  -H "Content-Type: application/json" \
  -d '{"suspend_until": "2024-12-31", "reason": "Violation of community guidelines"}'

HTTP/2 200
server: nginx
date: Mon, 11 Nov 2024 19:27:44 GMT
content-type: application/json; charset=utf-8
vary: Accept-Encoding
x-frame-options: SAMEORIGIN
x-xss-protection: 0
x-content-type-options: nosniff
x-permitted-cross-domain-policies: none
referrer-policy: strict-origin-when-cross-origin
x-discourse-username: michael
x-discourse-route: users/suspend
cache-control: no-cache, no-store
x-request-id: fe9c5ddc-b11a-45ba-ab41-9403eb53f255
cdck-proxy-id: app-router-tiehunter03.sea1
strict-transport-security: max-age=31536000
cdck-proxy-id: app-balancer-tieinterceptor1b.sea1

{
  "suspension": {
    "suspend_reason": "Violation of community guidelines",
    "full_suspend_reason": "Violation of community guidelines",
    "suspended_till": "2024-12-31T00:00:00.000Z",
    "suspended_at": "2024-11-11T19:27:44.927Z",
    "suspended_by": {
      "id": 85,
      "username": "michael",
      "name": "Michael Brown",
      "avatar_template": "/user_avatar/try.discourse.org/michael/{size}/639_2.png"
    }
  }
}
  • 验证 API 密钥
    使用该密钥进行简单的 GET 请求时,Discourse 是否显示有效的用户身份验证(x-discourse-username)?
  • 验证用户 ID
    它确实存在,对吗?如果不存在,它将返回 404。
  • 您是否在使用 Cloudflare?
    它可能会干扰参数。
2 个赞
  1. API 密钥验证
    已使用以下命令确认 API 密钥有效:
curl -X GET "https://redactedurl.com/admin/users/list/active.json" -H "Api-Key: LIVEPRODKEYHERE" -H "Api-Username: system"

响应(为保密已编辑敏感数据)如下:

[
  {
    "id": 1,
    "username": "User1",
    "name": "Redacted",
    "avatar_template": "/user_avatar/yourforum.com/user1/{size}/avatar.png",
    "active": true,
    "admin": true,
    "moderator": true,
    "trust_level": 4,
    "title": "Recognized Member",
    "days_visited": 6,
    "post_count": 3
  },
  {
    "id": 19,
    "username": "User2",
    "name": "Redacted",
    "avatar_template": "/letter_avatar_proxy/v4/letter/j/{size}.png",
    "active": true,
    "admin": false,
    "moderator": false,
    "trust_level": 1,
    "title": "Member (Unverified)"
  },
  {
    "id": 3,
    "username": "User3",
    "name": "Redacted",
    "avatar_template": "/user_avatar/yourforum.com/user3/{size}/avatar.png",
    "active": true,
    "admin": true,
    "moderator": true,
    "trust_level": 4,
    "title": "Community Founder"
  },
  {
    "id": -1,
    "username": "system",
    "name": "system",
    "avatar_template": "/user_avatar/yourforum.com/system/{size}/avatar.png",
    "active": true,
    "admin": true,
    "moderator": true,
    "trust_level": 4,
    "title": "Bot"
  }
]
  1. 用户存在确认
    测试用户(ID:15)已被验证存在,如通过查询用户数据所示,使用 /admin/users/15/suspend.json
{
  "id": 15,
  "username": "User15",
  "active": true,
  "admin": false,
  "moderator": false,
  "trust_level": 0,
  "days_visited": 1,
  "post_count": 0,
  "can_send_activation_email": true,
  "can_activate": false,
  "can_deactivate": true,
  "can_grant_admin": true,
  "can_grant_moderation": true,
  "can_impersonate": true,
  "groups": [
    {
      "name": "trust_level_0",
      "bio_excerpt": "New members with limited abilities"
    }
  ]
}

  1. 网络配置
    该网站未使用 Cloudflare。而是运行在 NGINX 反向代理后面。应用程序在内部监听端口 8080,NGINX 在外部访问时处理端口 443 上的 SSL 终止。app.yml 配置不直接绑定到端口 443,因为 SSL 和证书管理由 NGINX 处理,以确保安全连接和可用性。

你好,
我只是想跟进一下,我认为API密钥的全局范围中缺少暂停权限。我遇到了同样的错误。我不得不将我的密钥换成带有暂停权限的细粒度密钥才能使其工作。我也希望解除暂停功能可以正常工作,但似乎还没有通过API实现。