Setup Basic Authentication with REST API Authentication module
Basic Authentication is a very simple and straight-forward Authentication method. The client sends HTTP requests with an Authorization header that contains a base64-encoded username and password of your Drupal site.
In Basic Authentication, the miniOrange API authentication module will first authenticate the user against their Drupal username and password. If the user is authenticated, only then the user will be able to perform that operation. This module is compatible with Drupal 7, Drupal 8, Drupal 9, Drupal 10, and Drupal 11.
Setup Video:
Pre-requisites: Download and Installation:
- Download & install the Drupal REST & JSON API Authentication module.
- REST UI: This module provides you with a user interface for configuring the REST module.
- Enable the following Web Services modules from under the Extend section (/admin/modules) of your Drupal site:
- JSON: API
- REST UI
- RESTful Web Services
- Serialization
Steps to configure the Basic Authentication method in Drupal:
- For better understanding, we will be taking an example of adding Basic authentication for getting a node for Drupal using REST API.
- Please note that the /node/{node} API of Drupal is used to retrieve information about a Drupal node.
Enable the API, assign methods and operations as follows:
- The first step is to enable the API and also assign methods and operations allowed on that particular API. This can be done using the REST UI module or you can simply modify the config.
- To enable the API using the REST UI module, click on the configure button of the REST UI module(as shown below)
- Considering our example, we want to enable the /node/{node} API present under the Content section. Enable this API using the Enable option in front of it.
- Now, as our goal is to retrieve the node information, select the following configs:
- Method: GET
- Format: json
- Authentication provider: rest_api_authentication.
- This will allow the miniOrange REST API Authentication module to Authenticate your API. Click on the Save Configuration button to continue.
Steps to Enable the Basic Authentication:
- In this step, we will set up Basic Authentication as an API Authentication method. In order to do so, please navigate to the API Authentication tab of the REST API Authentication Module (/admin/config/people/rest_api_authentication/auth_settings)
- Select the Enable Authentication checkbox and click on Save above Settings.
- Below the Save Settings button select Basic Authentication and click on the select method button.
That’s it!!!
Now let’s try to fetch node information through an API call using Basic authentication.
Example:
- To get node information in Drupal, you have to make a GET request along with the Drupal username and password of the user. The value of the username and password must be in base64encoded format. You can refer to the below format to make a call.
Request: GET <your_drupal_base_url> /node/{node}?_format=json
Header:
Authorization: Basic base64encoded <username:password>
Accept: application/json
Content-Type: application/jsonCURL Request Format-
curl --location --request GET 'drupal_base_url/node/1?_format=json' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic base64encoded <username:password'>
- You can also refer to the image of the Postman request added below:
- A successful response returns the node information that you have created. (please refer to the image below)
- If you receive any error in response then you can refer to the below table for the error description and possible solutions.
Error |
Description |
MISSING_AUTHORIZATION_HEADER |
You will get this error whenever you don't send an Authorization Header in the API request or if it was removed by your server due to some reasons. Example: |
INVALID_AUTHORIZATION_HEADER_TOKEN_TYPE |
You will get this error when you send the Authorization header but not in a valid format. |
USER_NAME_MISSING |
You will get this error whenever the module is not able to find the username in the API call. Example: |
INVALID_CREDENTIALS |
You will get this error when either the username or password is incorrect. Example: |