【问题标题】:I receive {"code":"rest_forbidden","message":"Sorry, you are not allowed to do that.","data":{"status":401}} when trying to retrieve a private post我在尝试检索私人帖子时收到 {"code":"rest_forbidden","message":"Sorry, you are not allowed to do that.","data":{"status":401}}
【发布时间】:2019-10-04 18:17:25
【问题描述】:

尝试使用 REST API 检索帖子:

http://localhost/mysite/wp-json/wp/v2/posts/605.

我已通过管理员身份验证,并且该帖子是我发布的。我已分配所有管理员权限。我可以检索非私密帖子,但一旦我将它们标记为私密,我就会收到错误回复

{"code":"rest_forbidden","message":"Sorry, you are not allowed to do that.","data":{"status":401}}

我还应该做些什么来允许检索私人帖子吗?

-- 编辑:我通过 JWT 进行身份验证,但我的回复返回所有公开帖子,没有私人帖子

if (await client.IsValidJWToken())
                {                  
                    var queryBuilder = new PostsQueryBuilder();
                    queryBuilder.PerPage = 8;
                    queryBuilder.Page = 1;
                    queryBuilder.Embed = true;
                    //queryBuilder.Categories = new int[] { category };
                    queryBuilder.Statuses = new Status[] { };

                    try
                    {
                        var response = await client.Posts.Query(queryBuilder);
                        var r = response;

                    }
                    catch (Exception e)
                    {
                        var m = e.Message;

                    }

【问题讨论】:

  • 没有看到您究竟是如何尝试检索帖子的,我们无法提供太多帮助,因此请提供更多详细信息(例如,minimal, reproducible example of your code 会很好。)
  • 这可能是权限问题。你知道如何使用调试器吗?它可以准确地向您展示请求被拒绝的原因。我认为您不需要编写代码。但是,请准确找出缺少的权限。
  • “我已通过管理员身份验证” - 登录到该站点不会自动将您登录到 REST API 端点。默认情况下,查看私人帖子需要authentication - 您是否验证了您的请求?
  • @SallyCJ 是的,我编辑了原始帖子以添加代码。我可以检索所有公开帖子,但私人帖子不会出现在列表中。即使我不使用查询生成器,也会发生同样的事情。
  • @Orbiter, "don't show up in the list" - 如果您要检索帖子列表,则需要明确指定帖子状态,除非您只是检索帖子除了经过身份验证外,还发布了。例如,可以请求example.com/wp-json/wp/v2/posts?status=publish,draft。您是否尝试过设置状态?

标签: wordpress wordpress-rest-api


【解决方案1】:

我认为这是预期的行为 [至少我可以在我的网站上确认同样的事情]。您可能必须创建自己的路径才能访问私人帖子。

【讨论】:

  • 感谢您的快速回复。你知道最快的方法是什么吗?我还可以删除和更新私人帖子
猜你喜欢
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
  • 2013-07-29
  • 2021-03-19
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多