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

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?

2 个赞

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?

1 个赞

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.

1 个赞

现在还有其他人在遇到这个问题吗?我这两天一直在尝试使用 API,但每次都遇到类似的错误。我不确定是自己漏掉了某个步骤,还是有什么地方发生了变化。

我为 system 用户创建了一个新的 API 密钥。我也尝试为所有用户创建 API 密钥,然后使用我的管理员账户。

使用 discourse_api Ruby gem 时出现以下错误:

DiscourseApi::UnauthenticatedError ({"errors"=>["您无权查看请求的资源。API 用户名或密钥无效。"], "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 "成功!"
  else
    puts "错误"
    puts response.code
  end
end

send_request # 400

我还尝试将 api_usernameapi_key 作为表单数据与原始请求一起使用 API,但收到了 404 错误。

我正在尝试编写一个脚本,读取电子邮件地址列表,获取用户名,然后将他们取消订阅所有 Discourse 邮件。

针对您在使用原始 Ruby 代码时遇到的错误,问题在于 Api-KeyApi-Username 需要设置在请求头中。API 字段名也必须使用连字符(-)而不是下划线(_)。请查看 https://docs.discourse.org/ 顶部的“认证”部分,了解如何对请求进行认证的详细信息。

我不确定使用 Discourse API gem 进行调用时出现错误的具体原因。最可能的原因是您使用的 API 凭据不正确。请确保您使用的 API 密钥的用户级别已设置为“所有用户”:

如果这未能解决问题,请在此处发布您尝试发起的请求,我会帮您查看。

5 个赞

谢谢,这样可以。我之前是按照这里的前几篇帖子来操作的,这些内容可能需要更新:

2 个赞

感谢指出。我已更新了原帖(OP),并删除了该主题中最旧的帖子。

3 个赞