我正在我的Wordpress站点上设置REST,但是我一直得到一个:
"rest_forbidden",“讯息”:“对不起,你不能这么做。”
消息同时存在于普通用户和我的管理员帐户上。
我只是不明白。
我试着在阅读关于这个问题的不同文章时做作业,我得出的结论是,我的用户没有"manage_options“权限。即使是管理员,这对我来说也是一个难题,因为它应该作为一个标准。
我试图通过以下两篇文章修复这个错误:
403 "rest_forbidden" error in WordPress REST API (but only for settings)?
我需要帮助!
我的JS代码如下所示:
$.ajax({
json/agility_body_reactions/v1/exercise_data_submits',
url: 'https://MySite.dk/wp-json/agility/v1/body_reactions_exercise_submits/',
method: 'POST',
beforeSend: function(xhr) {
xhr.setRequestHeader(
'X-WP-Nonce',
wpApiSettings.nonce );
},
data: {
gender: gender,
age: age,
minutes: minutes,
seconds: seconds
}
});
我的代码如下:
add_action('rest_api_init', 'register_endpoint_body_reaction');
function register_endpoint_body_reaction()
{
register_rest_route(
'agility/v1',
'/body_reactions_exercise_submits/',
array(
'methods' => 'POST',
'callback' => 'callback_body_reaction',
'args' => array(
'age' => array(
'required' => true,
'validate_callback' => function($param, $request) {
return is_numeric( $param) and ! is_null( $param);
},
'sanitize_callback' => 'absint'
),
'minutes' => array(
'required' => true,
'validate_callback' => function($param, $request) {
return is_numeric( $param) and ! is_null( $param);
},
'sanitize_callback' => 'absint'
)
)
,
'permission_callback' => function() {
if ( !is_user_logged_in() ) {
return new WP_Error( 'Unauthorized', 'Sorry, but your not logged in...fll', array( 'status' => 401 ) );
}
}
)
);
}
我的Enqueue脚本代码如下所示:
add_action( 'wp_enqueue_scripts', 'enqueue_js_body_reaction');
function enqueue_js_body_reaction()
{
if (!is_page('agility-body-reaction')) {
return;
}
wp_enqueue_script(
'agility_body_reaction',
plugins_url( '../js/agility_body_reaction.js', __FILE__ ),
array( 'jquery', 'jquery-ui-core' ),
AGILITY_BODY_REACTION_VERSION,
true
);
wp_localize_script(
'agility_body_reaction',
'wpApiSettings',
array(
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' ),
)
);
}
所以我的问题是:
谢谢。
弗莱明
更新!
我向注册终结点Permission_callback添加了一些代码:
if ( !current_user_can( 'manage_options' ) ) {
return new WP_Error( 'Unauthorized', 'Sorry, but you dont have the manage_options permissions...fll', array( 'status' => 401 ) );
}
然后,我用我的管理员和普通用户尝试了REST。
管理员得到一个403,这意味着它有"manage_options“集。普通用户获得上述消息,这意味着它没有"manage_options set“。
我猜这意味着我的REST还有其他问题。不过,我仍然需要知道如何为普通用户启用"manage_options“。
更新!
我在一些文章中读到,.htaccess可以创建一个"403禁忌“,但我真的不熟悉这个文件。我非常绝望,所以我会发布.htaccess配置。
# BEGIN Really Simple SSL Redirect 5.3.0
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END Really Simple SSL Redirect
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<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>
# END WordPress
# Wordfence WAF
<IfModule LiteSpeed>
php_value auto_prepend_file '/var/www/<MySite>/public_html/wordfence-waf.php'
</IfModule>
<IfModule lsapi_module>
php_value auto_prepend_file '/var/www/<MySite>/public_html/wordfence-waf.php'
</IfModule>
<Files ".user.ini">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
# END Wordfence WAF
发布于 2022-11-13 21:20:21
我发现了错误。
使用is_user_logged_in的代码有一个错误。
'permission_callback' => function() {
if ( !is_user_logged_in() ) {
return new WP_Error( 'Unauthorized', 'Sorry, but your not logged in...fll', array( 'status' => 401 ) );
}
}
它应该像这样记录:
'permission_callback' => function() {
return is_user_logged_in();
}
因此,除了这些代码之外,所有代码都可以用于基于cookie的身份验证。
https://stackoverflow.com/questions/74335215
复制相似问题