[API] Create Application Passwords (ok)
https://maheshwaghmare.com/doc/application-passwords/
Last updated
https://maheshwaghmare.com/doc/application-passwords/
Last updated
Đọc thêm plugin ở đây :)
https://github.com/WP-API/authentication/issues/13
In this article we are going to see all about the WordPress plugin Application Passwords.
Create different applications with unique passwords for any user to manage the REST API & XML requests.
Table of Content
In simple basic authentication REST API & XML-RPC request we use the username and the password of the WordPress user.
Application Passwords provide a way to create multiple applications for each user to perform REST API authenticate requests without using users’ original passwords.
We have another plugin JSON Basic Authentication which allows us to perform basic authentication request. But JSON Basic Authentication uses the actual user password which is basically not useful for the live site. Read more about performing basic authentication requests with JSON basic authentication plugin.
NOTE: Application Passwords is a Beta plugin in WordPress. So, I recommend avoiding using it on production sites. See all beta plugins in WordPress.
The application password generate a unique password for each application.
We can able to create multiple applications for each user.
We can remove any application for any time.
No need for users’ original password for authentication.
NOTE: Application Passwords are ONLY used for authenticating REST API and XML-RPCrequests. It will not work for regular site login.
Follow below steps to install the Application Passwords.
Go to Plugins > Add new
Search for Application Passwords
Click on Install Now
and then click on Activate button
To create a application password follow below steps:
Go to Users > Your Profile
Navigate to the Application Passwords section
In input field add your application name. I have name my application as “Example App“
Click on Add New
Here my application password is generated as: mK7M wZmN Fuj5 IKYF XUAv EZ8H
NOTE: Save this application password. Because we’ll not see this anymore. If somehow you close it then you need to create new one.
Lets see how to use the Application Passwords with practical examples.
Here, I’m using Postman, CURL & wp_remote_post to demonstrate how we can create a new post with Rest API request.
Open Postman
Select GET method
Use your website URL http://example.com/wp-json/wp/v2/posts/
Click on Send
Here, We have not used Application Password anywhere. We have just get the list of all posts to check the Rest API is enabled or not on our website.
Most of the times Rest API is disabled by security plugins. So, you can enable the Rest API support form them.
We have now created a application passwords. So we can use it to send the Basic authentication request.
In this example we are going to create a new post with Basic authentication.
To create the post we can need to use endpoint http://example.com/wp-json/wp/v2/posts/.
In this example i’m just using only the title
field to create a new post title.
You can see all available fields at – https://developer.wordpress.org/rest-api/reference/posts/#create-a-post
Lets see how to do it.
Set the POST method
Add endpoint with title parameter and add post title like – /wp-json/wp/v2/posts/?title=Rest API Post 1
Select Authentication type Basic auth
Add your username
Add your application password
Note: Here the username is which you use to login to your website. And application password is newly created application password. Im my case it is mK7M wZmN Fuj5 IKYF XUAv EZ8H
.
Click on Send button.
Now open your draft
posts list. You can see our newly created post there.
Syntax
Example
curl --user "admin:mK7M wZmN Fuj5 IKYF XUAv EZ8H" -X POST -d "title=Rest API Post 2" http://localhost/dev.test/wp-json/wp/v2/posts/
view rawcurl-request.bat hosted with by GitHub
Output:
Code:
<?php
if( ! function_exists( 'prefix_create_new_post' ) ) :
/**
* Create new post with wp_remote_post()
*
* @since 1.0.0
* @return void
*/
function prefix_create_new_post() {
$username = 'admin';
$password = 'mK7M wZmN Fuj5 IKYF XUAv EZ8H';
$site_url = 'http://localhost/dev.test/';
$request = wp_remote_post( $site_url . 'wp-json/wp/v2/posts/', array(
'body' => array(
'title' => 'Rest API Post 3',
),
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
),
) );
}
add_action( 'admin_head', 'prefix_create_new_post' );
endif;
view rawbasic-auth-request.php hosted with by GitHub
If somehow your server have not added the HTTP Authentication support then you can see something below error:
Due to a potential server misconfiguration, it seems that HTTP Basic Authorization may not work for the REST API on this site: Authorization
headers are not being sent to WordPress by the webserver. You can learn more about this problem, and a possible solution, on our GitHub Wiki.
Solution
We need to add it from yourself into the .htaccess file. Add below code into your
After adding above code into .htaccess file we can use any application passwords.
Performing Basic Authentication Rest API requests in WordPress with Application Passwords.Tweet
See below screenshot for reference:Plugin Installation Screen
Checkout below screenshot for reference:Create a New Application
After clicking on Add new we can see the popup which show the message something like below:Application Successful Creation Popup
Open Postman
You will see something like:Get All Posts
you can see something likeCreate a New Post Request
E.g.Posts List Screen
E.g.Terminal API Request
OutputPosts List Screen
Output –Posts List Screen