How can I allow users to like posts using RESTapi

Hi all. I am pretty new to Discourse and checked All Site Settings (/admin/site_settings) section to find a matching option to allow any level of user to like posts using RESTapi

At the moment, all users can ( without public ) create topics, add replies etc. However a normal user (tl0, tl1 etc) cannot like to any post through the API ( But can like using the web panel )

Here’s is my category settings,

I’m using following code to add a like through API

  public async likeOrThrow(contentId: number, username: string) {
    try {
      this.overrideUsername(username);
      const res = await this.client.post(`post_actions.json`, {
        id: contentId,
        post_action_type_id: 2,
        flag_topic: false,
      });
      return res.data;
    } catch (error) {
      throw this.getException(error);
    } finally {
      this.resetUsername();
    }
  }

It throws,

You are not permitted to view the requested resource.

However if i log into the discourse as the user then that user can make a like,

So far I have integrated, Topic create, Post Create & Bookmark APIs with same API key with same categories and username and they work without any permission issue.

Could you please help me to figure out what im doing wrong here ? Let me know if more information is needed.

Thanks

1 Like

I think you’ll need

I don’t know, but you can make your category settings easier using only everyone.

2 Likes

Hi @Bas thanks for the comment,

but why only for like ? I already implemented post creation, reply creation, bookmark add, remove with the Discourse API using Global key ( user level: all-users, scope: global )

Also tried to create a new Global API key, still getting the same error :frowning:

If the API key has global scope and valid for all users, then following API should work. isn’t it

https://docs.discourse.org/#tag/Posts/operation/performPostAction

Yes, this code looks good to me as well.

How / to what is contentId being set ? Is it the correct Post.id ?

Hi @RGJ

contentId is the post id and yes it is the correct post id.

With the following code, it logs,

      this.client.interceptors.request.use((config) => {
        Logger.log(`Request to ${config.url}`);
        Logger.log(`Headers: ${JSON.stringify(config.headers, null, 2)}`);
        Logger.log(`Body: ${JSON.stringify(config.data, null, 2)}`);
        return config;
      })

Log,

[Nest] 97134  - 12/02/2024, 4:10:29 PM     LOG Request to post_actions.json
[Nest] 97134  - 12/02/2024, 4:10:29 PM     LOG Headers: {
  "Accept": "application/json, text/plain, */*",
  "Api-Username": "m...91",
  "Api-Key": "66dc....18b56fbc17bcf"
}
[Nest] 97134  - 12/02/2024, 4:10:29 PM     LOG Body: {
  "id": 84,
  "post_action_type_id": 2,
  "flag_topic": false
}

Oh snap, I think I found the issue. :grimacing:

When i did the test using web panel, i mistakenly choose a post from another user ( Same title as i was testing ) and it succeeded.

But when i test it using my system, i was trying to add a like to an own post.

Apologies for any inconvenience. I did not aware that it is not possible to like own posts.

Thanks and have a nice day.

3 Likes