APIで404エラーが頻発しています

こんにちは、Discourse コミュニティの皆様、

現在、Discourse API エンドポイント、特にユーザーの一時停止に関連する問題に直面しており、皆様からの支援やガイダンスをいただければ幸いです。

問題の概要

  1. 404 Not Found

    • エンドポイント /admin/users/15/suspend.json を使用すると、ユーザーは存在しているにもかかわらず、一貫して「ページが見つかりません」というエラーが発生します。
    • 代替エンドポイント /admin/u/15/suspend.json も試しましたが、同じ問題が発生します。
  2. 予期しない HTML レスポンス

    • 期待される JSON レスポンスの代わりに、API は「おっと!そのページは存在しないか、プライベートです。」というメッセージが表示された HTML ページを返します。
    • これは、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 例

# Discourse でユーザーを一時停止するための PowerShell スクリプト

$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 "User suspended successfully!" -ForegroundColor Green
} catch {
    Write-Host "Error suspending user:" -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"
      }
    ]
    
  2. ユーザー存在の確認
    テストユーザー(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"
        }
      ]
    }
    

  3. ネットワーク構成
    サイトは Cloudflare を利用していません。代わりに、NGINX リバースプロキシの背後で動作しています。アプリケーションは内部でポート 8080 でリッスンし、NGINX は外部アクセス用にポート 443 で SSL 終端を処理します。SSL および証明書管理は NGINX によって処理され、セキュアな接続と可用性を確保するため、app.yml 構成はポート 443 に直接バインドされません。

こんにちは。

これについてフォローアップですが、APIキーのグローバルスコープにサスペンドスコープが欠けていると思います。私も同じエラーに遭遇していました。機能させるには、サスペンドスコープを持つ詳細なキーにキーを切り替える必要がありました。また、サスペンド解除が機能することを期待していましたが、APIではまだ利用できないようです。