.htaccess WordPress应用程序错误:抱歉,不允许您创建新帖子,

bvjveswy  于 2022-11-16  发布在  WordPress
关注(0)|答案(2)|浏览(291)

我正在使用当前的应用程序脚本功能添加一个新的职位到我的WordPress站点:

var um_url = 'http://test.mywebsite.com/wp/wp-json/wp/v2/posts';
 var um_headers = { 
    "Accept" : "application/json",
    "Authorization": "Basic "+Utilities.base64Encode("user:passowrd"),
    "Content-type":"application/json"
        };

 var um_options = {  
   "method":"POST",     
   "headers": um_headers,
   "dataType" : 'json',
   "data": {
     title: "Foo title",
     content: "Foo content",
     status: "publish"
    }

    };

  var um_response = UrlFetchApp.fetch(um_url, um_options);
  var json = um_response.getContentText();
  Logger.log(json);
}

但我得到这个错误:

{"code":"rest_cannot_create","message":"Sorry, you are not allowed to create new posts.","data":{"status":401}}

我读了其他类似的StackOverflow问题,他们通过改变.htacess文件解决了它,不幸的是,这并没有解决我的问题。这是我的.htaccess文件

# BEGIN WP BASIC Auth
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
</IfModule>
# END WP BASIC Auth


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteBase /
RewriteRule ^index\.php$ - [E=X-HTTP_AUTHORIZATION:%        {HTTP:Authorization},QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
</IfModule>

# END WordPress

有人能帮忙吗?

mpgws1up

mpgws1up1#

我刚刚遇到了几乎完全相同的问题,试图通过REST API添加一些东西。诀窍是你需要将REST WP Nonce(名为wp_rest)与请求一起发送,作为头部,或者作为请求变量的一部分...
如果使用fetch,则可以执行以下操作...

fetch( route, {
    method: method, 
    headers: {
        'X-WP-Nonce': wpApiSettings.nonce
    }
} )

或将其添加到查询变量中,如**'_ wpnence'**
您可以在第834行(版本5.2.1)wp-includes/rest-api.php处找到将当前用户设置为0的代码

vktxenjb

vktxenjb2#

这是因为您的数据对象中存在status: "publish"。将其更改为status: "pending"即可。这是因为用户无法直接发布帖子,除非他们拥有author或更高的角色。
您可以如下更改代码(或者)将用户角色更改为author,并将New User Default Role设置为WordPress settings » General下的Author(对于新用户)。

var um_url = 'http://test.mywebsite.com/wp/wp-json/wp/v2/posts';
 var um_headers = { 
    "Accept" : "application/json",
    "Authorization": "Basic "+Utilities.base64Encode("user:passowrd"),
    "Content-type":"application/json"
        };

 var um_options = {  
   "method":"POST",     
   "headers": um_headers,
   "dataType" : 'json',
   "data": {
     title: "Foo title",
     content: "Foo content",
     status: "pending"
    }

    };

  var um_response = UrlFetchApp.fetch(um_url, um_options);
  var json = um_response.getContentText();
  Logger.log(json);
}

相关问题