API Error: You are not permitted to view the requested resource

AI-generated summary

A user, erik_chan, is experiencing an error (“You are not permitted to view the requested resource”) when trying to access a private category using the Ruby API. They have tried authenticating with different users, including the system user, but still encounter the issue.

erik_chan mentions that they expect users with admin rights to be able to access private categories via the API.

Other users, such as chenny408 and j127, report similar issues.

simon suggests that the problem might be due to incorrect API credentials or authentication method. They point out that the Api-Key and Api-Username need to be set in the request headers, and the API field names should use dashes (-) instead of underscores (_).

j127 confirms that setting the Api-Key and Api-Username in the request headers resolves the issue. They also mention that the Discourse API documentation might need to be updated to reflect this.

simon updates the relevant documentation and deletes outdated posts.

I am receiving the following error:

DiscourseApi::UnauthenticatedError: 
{"errors"=>["You are not permitted to view the requested resource."], "error_type"=>"invalid_access"}

when I make the ruby API call ‘category_latest_topics’

response = client.category_latest_topics("<some_category>") 

This is due to the category permissions (the category is not public). I tried to authenticate by using

client.api_username = "system"  

and making sure “system” is a member of the group that has permissions to the category but still no luck! Any suggestions or workarounds?

No response so far. Am I posting this in the right forum?

Try using a different user, other than System. Issue an API key for a different user. You will need to experiment.

No luck. Tried issuing API key for different users who have permission to the category but still no access.

Would be awesome if anyone else has suggestions for this?

How can I access a private category via the API. This should not be an issue for users/system with admin rights to the forum.

I’m having the same issue, can someone take a look into this?

Have you attempted to generate a key from your account? You are the admin who created the forum, yes? You say “users/system” so I am wondering if you tried your own account.

Yes i am the admin of my forum.

I have tried both my username and system with no luck. I can’t call client.category_latest_topics(<category_name>) even though the user has permissions to the category

I feel some other variable exists and is causing this error; one that cannot be seen due to the limited info provided.

Be sure to check these topics in regards to setting up and using the Discourse API, if you have not yet done so.

Appreciate your help. If you read my original post i am using the ruby api. I have also looked through the documentation before posting this topic.

If you could show me what variable i am missing or where to exactly to find out more i would appreciate it.

現在、同じ問題に直面している方はいますか?API を使おうとして 2 日間試していますが、毎回同様のエラーが発生します。手順を見落としているのか、それとも何かが変更されたのか分かりません。

system ユーザー用に新しい API キーを作成しました。また、すべてのユーザー用に API キーを作成し、その後で管理者アカウントを使用することも試みました。

discourse_api Ruby gem を使用した場合:

DiscourseApi::UnauthenticatedError ({"errors"=>["You are not permitted to view the requested resource. The API username or key is invalid."], "error_type"=>"invalid_access"})

別の投稿から以下のコードも試しました:

require 'net/http'
require 'uri'

INSTANCE_URL = 'https://forum.example.com/admin/users/list/all.json?email=user@example.com'
API_USERNAME = 'system'
API_KEY = 'a_new_api_key_here'

def send_request
  url = URI.parse(INSTANCE_URL)
  request = Net::HTTP::Get.new(url.path)
  request.set_form_data({'api_username' => API_USERNAME, 'api_key' => API_KEY})
  http = Net::HTTP.new(url.host, url.port)
  response = http.request(request)

  if response.code == '200'
    puts "Success!"
  else
    puts "Error"
    puts response.code
  end
end

send_request # 400

また、api_username と api_key をフォームデータに含めて生リクエストを送信する方法も試しましたが、404 エラーが発生しました。

メールアドレスのリストを読み込み、ユーザー名を取得し、その後でそのユーザーをすべての Discourse メール購読から解除するスクリプトを作成しようとしています。

生の Ruby コードで発生しているエラーについてですが、問題は Api-Key と Api-Username をリクエストヘッダーに設定する必要がある点です。また、API フィールド名はアンダースコア(_)ではなくハイフン(-)を使用する必要があります。詳細については、https://docs.discourse.org/ の上部にある「Authentication」セクションを参照して、リクエストの認証方法を確認してください。

Discourse API gem を使用して呼び出しを行った際にエラーが発生する原因については、現時点では特定できていません。最も考えられる原因は、正しい API 認証情報を使用していないことです。ユーザーレベルが「All Users」に設定された API キーを使用しているか確認してください。

それでも問題が解決しない場合は、実行しようとしているリクエストをここに投稿してください。確認いたします。

ありがとうございます、動作しました。最初の数件の投稿こちらを参考にしていたのですが、更新が必要かもしれません:

ご指摘ありがとうございます。OP を更新し、トピックから最も古い投稿を削除しました。