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

**URL:** https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035
**Category:** Development
**Tags:** rest-api
**Created:** [October 28, 2015, 10:30pm UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035 "2015-10-28T22:30:08Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![erik\_chan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/erik_chan/32/115850_2.png) [@erik\_chan](https://meta.discourse.org/u/erik_chan)
#### Post date: [October 28, 2015, 10:30pm UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/1 "2015-10-28T22:30:09Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![erik\_chan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/erik_chan/32/115850_2.png) [@erik\_chan](https://meta.discourse.org/u/erik_chan)
#### Post date: [October 30, 2015, 3:12am UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/2 "2015-10-30T03:12:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [October 30, 2015, 3:49am UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/3 "2015-10-30T03:49:46Z")

</div>

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

---

<div class="post-metadata">

### Author: ![erik\_chan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/erik_chan/32/115850_2.png) [@erik\_chan](https://meta.discourse.org/u/erik_chan)
#### Post date: [October 30, 2015, 4:18am UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/4 "2015-10-30T04:18:17Z")

</div>

> [@codinghorror](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![erik\_chan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/erik_chan/32/115850_2.png) [@erik\_chan](https://meta.discourse.org/u/erik_chan)
#### Post date: [November 1, 2015, 8:15pm UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/5 "2015-11-01T20:15:49Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![chenny408](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@chenny408](https://meta.discourse.org/u/chenny408)
#### Post date: [November 4, 2015, 2:58am UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/6 "2015-11-04T02:58:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![purldator](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@purldator](https://meta.discourse.org/u/purldator)
#### Post date: [November 5, 2015, 5:27am UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/7 "2015-11-05T05:27:28Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![erik\_chan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/erik_chan/32/115850_2.png) [@erik\_chan](https://meta.discourse.org/u/erik_chan)
#### Post date: [November 5, 2015, 5:41am UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/8 "2015-11-05T05:41:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![purldator](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@purldator](https://meta.discourse.org/u/purldator)
#### Post date: [November 5, 2015, 5:53am UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/9 "2015-11-05T05:53:58Z")

</div>

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.

> [@Use the Discourse API ruby gem](https://meta.discourse.org/t/using-the-discourse-api-ruby-gem/17587):
>
> So you want to use [Discourse API](https://meta.discourse.org/t/discourse-api-documentation/22706)? Great! Let’s get started. Set up Discourse development environment Set up Discourse development environment using our [Windows](https://meta.discourse.org/t/install-discourse-on-windows-for-development/75149), [macOS](https://meta.discourse.org/t/install-discourse-on-macos-for-development/15772/) or [Ubuntu](https://meta.discourse.org/t/install-discourse-on-ubuntu-or-debian-for-development/14727/) guide. Clone Discourse API Gem Now that you have set up Discourse development environment, you should already have Git and Ruby installed on your system. You can install Discourse API gem by running following command from console: git clone https://github.com/discourse/discourse\_api.git ~/discourse\_api Alternatively…

> [@Discourse REST API Documentation](https://meta.discourse.org/t/discourse-api-documentation/22706):
>
> Discourse API Please view the Discourse API Documentation site for detailed info: [https://docs.discourse.org](https://docs.discourse.org)information_source Authentication API requests must use HTTP header based authentication. Pass your Api-Key and Api-Username as HTTP headers. Authentication via query parameters or request body is not supported (this was removed in April 2020). Please see the example cURL request below. The only API endpoints that continue to support credentials in query parameters are requests to…

> [@Reverse engineer the Discourse API](https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576):
>
> Discourse is backed by a complete JSON api. Anything you can do on the site you can also do using the JSON api. The API is documented at [docs.discourse.org](https://docs.discourse.org). You can also use the [discourse\_api](https://github.com/discourse/discourse_api) Ruby gem as a client library. However, not every endpoint is documented. To determine how to do something with the JSON API here are some steps you can follow. Example: recategorize a topic. Go to a topic and start editing a category: Open Chrome dev tools, switch to the Network tab, select …

---

<div class="post-metadata">

### Author: ![erik\_chan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/erik_chan/32/115850_2.png) [@erik\_chan](https://meta.discourse.org/u/erik_chan)
#### Post date: [November 5, 2015, 6:15am UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/10 "2015-11-05T06:15:06Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![j127](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j127/32/79093_2.png) [@j127](https://meta.discourse.org/u/j127)
#### Post date: [June 19, 2020, 8:28pm UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/13 "2020-06-19T20:28:28Z")

</div>

Is anyone else experiencing this problem at the moment? I’ve been trying for two days to use the API, but I get similar errors every time. I’m not sure if I’m missing a step or if something changed.

I created a new API key for the `system` user. I also tried creating an API key for all users and then using my admin account.

Using the `discourse_api` Ruby gem:

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

```

I also tried with this code from another post:

```ruby
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

```

I also tried using the API by putting the `api_username` and `api_key` in form data along with a raw request, but I get a 404 error.

I’m trying to write a script that reads in a list of email addresses, gets the usernames, and then unsubscribes them from all Discourse emails.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [June 19, 2020, 10:45pm UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/14 "2020-06-19T22:45:41Z")

</div>

For the error that you’re getting with the raw ruby code, the problem is that the `Api-Key` and `Api-Username` need to be set in the request headers. The API field names also need to use dashes (`-`) instead of underscores (`_`). Have a look at the Authentication section at the top of [https://docs.discourse.org/](https://docs.discourse.org/) for details about how to authenticate the request.

I’m not sure what would be causing the error when you make the call with the Discourse API gem. The most likely cause of the problem is that you aren’t using the correct API credentials. Make sure that you are using an API key that has its User Level set to All Users:

 ![image](https://global.discourse-cdn.com/meta/original/3X/3/d/3d7f218652b86c34abd55dff4c4b2204d31baf26.png)

If that doesn’t solve the problem for you, post the request that you’re trying to make here and I’ll have a look at it.

---

<div class="post-metadata">

### Author: ![j127](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j127/32/79093_2.png) [@j127](https://meta.discourse.org/u/j127)
#### Post date: [June 19, 2020, 11:18pm UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/15 "2020-06-19T23:18:45Z")

</div>

Thanks, that works. I was following the first couple of posts [here](https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576), which might need to be updated:

> [@](#):
>
> Equip with this information you can make your own calls using your favorite programming language, all you need to do is add **api\_username** and **api\_key** to parameters to the request.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [June 19, 2020, 11:55pm UTC](https://meta.discourse.org/t/api-error-you-are-not-permitted-to-view-the-requested-resource/35035/16 "2020-06-19T23:55:52Z")

</div>

> [@j127](#):
>
> I was following the first couple of posts [here](https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576), which might need to be updated:

Thanks for pointing that out. I’ve updated the OP and deleted oldest posts from the topic.
