[API] Create Application Passwords (ok)
https://maheshwaghmare.com/doc/application-passwords/
Đọ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
#Overview Overview
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.
#Features Features
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.
#How to install? How to install?
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
#Create an application for the user? Create an application for the user?
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.
#Examples of Application Passwords Examples of Application Passwords
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.
#Create a post with Postman Create a post with Postman
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.
#Create a post with CURL Create a post with CURL
Syntax
Example
Output:
#Create a post with wp_remote_post Create a post with wp_remote_post
Code:
#Common Error & Solution Common Error & Solution
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
Like this:
Last updated