我正在嘗試在我的 Wordpress 網站上設定 REST API,但我不斷收到:
403 "rest_forbidden","message": "Sorry, you are not allowed to do that."
普通用戶和我的管理員帳戶上的訊息。
我只是不明白。
我嘗試閱讀有關此事的不同文章來做功課,得出的結論是我的用戶沒有“manage_options”權限。即使是管理員,這對我來說也是一個難題,因為它應該被授予標準。
我嘗試按照以下兩篇文章來修復錯誤:
https://wordpress.stackexchange.com/questions/348231/how-do-i-correctly-setup-an-ajax-nonce-for-wordpress-rest-api/348239#348239
WordPress REST API 中的 403“rest_forbidden”錯誤(但僅針對設定)?
我需要一些幫助!!!
我的 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 ) );
}
}
)
);
}
我的排隊腳本代碼如下所示:
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' ),
)
);
}
所以我的問題是:
- 如何將正確的權限“manage_option”添加到用戶?
- “manage_option”似乎有很多權利授予普通用戶,即使它是短時間的,難道沒有另一種方法可以僅使用普通訂閱者權限運行 REST API 嗎?
謝謝。
弗萊明
更新!
我向注冊端點 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 API。
管理員收到 403,這意味著它設定了“manage_options”。普通用戶收到上述訊息,這意味著它沒有“manage_options set”。
我想這意味著我的 REST API 存在一些其他問題。不過,我仍然需要知道如何為普通用戶啟用“manage_options”。
更新!
我在一些文章中讀到 .htaccess 可以創建“403 Forbidden”,但我真的不熟悉這個檔案。我非常絕望,所以我將發布 .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
uj5u.com熱心網友回復:
我發現了錯誤。
帶有 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://www.uj5u.com/qianduan/534344.html
