Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP REST API and "Only authenticated users can access the REST API"

I have a WordPress: https://example.com I need to use WP REST API.

In .htaccess:

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

I use theBasic-Auth plugin on my WordPress: https://github.com/WP-API/Basic-Auth

Nevertheless I have:

{
    "code": "rest_cannot_access",
    "message": "Only authenticated users can access the REST API.",
    "data": {
        "status": 401
    }
}

enter image description here

I tested with http and https.

What's wrong?

like image 994
cmii Avatar asked Aug 18 '26 11:08

cmii


2 Answers

Solved!

I changed it to the following

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

The HTTP_AUTHORIZATION rule has to come before the other rules, this is because the L flag exists, the L flag means (last - stop processing rules), because of this it would never come to that rule if it was after the original wordpress rules,

like image 90
cmii Avatar answered Aug 20 '26 00:08

cmii


I had the same issue and fixed it by verifying whether there is any other Password protection plugin. I was using such plugin named, Password Protected and disabled it to generate the authentication token.

like image 38
Tayyab Chaudhary Avatar answered Aug 20 '26 00:08

Tayyab Chaudhary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!