您好 Discourse 社区,
我目前在使用 Discourse API 端点时遇到了一些挑战,特别是与暂停用户相关的操作,非常希望能得到您的帮助或指导。
问题摘要
-
404 未找到:
- 在使用端点
/admin/users/15/suspend.json时,即使用户存在,我也会一直收到“页面未找到”的错误。 - 我也尝试了备用端点
/admin/u/15/suspend.json,但结果相同。
- 在使用端点
-
意外的 HTML 响应:
- API 返回的是一个 HTML 页面,显示消息“哎呀!该页面不存在或已设为私有。”,而不是预期的 JSON 响应。
- 无论我使用
curl还是 PowerShell 进行请求,都会出现此问题。
-
API 密钥和权限:
- 使用的 API 密钥具有完整的管理员访问权限,但请求仍然返回
404错误。 - 我也尝试使用精细控制的密钥(仅限于暂停操作)和只读密钥,但问题依然存在。
- 有趣的是,通过浏览器访问该端点时工作正常,但使用带有密钥的 API 请求时却失败了。
- 使用的 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-Agent和Referer,但均无效。
非常感谢您抽出宝贵时间提供任何见解或建议来帮助解决这些问题!
此致,
Ross