Looking for some help with cookie auth
-
Hello!
I am building a little plugin to teach myself about the API, and I’m getting stuck when it comes to authentication.
I am doing localize script:
$nonce = wp_create_nonce( 'wp_json' ); ... $params = array( ... 'nonce' => $nonce, ); wp_localize_script( 'sjf_tjra', 'sjf_tjra', $params );
And then in my JS:
nonce = sjf_tjra.nonce; // Send the data to the server. var ajaxData = { ... nonce: nonce }; // Make the ajax call. $.get( sjf_tjra.ajaxurl, ajaxData, function( ajaxResult ) { ... }
And then back in my php, in my ajax callback function, I am doing
$nonce = $_REQUEST[ 'nonce' ]; ... $headers = array( 'X-WP-Nonce' => $nonce, ); $args = array( ... 'headers' => $headers, ); $request = wp_remote_request( $url, $args );
If I var dump args, there, I get
array(3) {
[“method”]=> string(4) “POST”
[“body”]=> array(1) {
[“data”]=> object(stdClass)#31 (2) {
[“title”]=> string(9) “hello 124”
[“content_raw”]=> string(19) “this is the content”
}
}
[“headers”]=> array(1) {
[“X-WP-Nonce”]=> string(10) “2c43c116bb”
}
}
0But my response from the API is
[{“code”:”json_cannot_create”,”message”:”Sorry, you are not allowed to post on this site.”}]
Sorry if this is hard to follow. I”m trying really hard to obey the docs at https://wp-api.org/guides/authentication.html#cookie-authentication but I must be missing something.
- The topic ‘Looking for some help with cookie auth’ is closed to new replies.