Skip to content

Accessing AuthAction Management API with M2M Application

This will explain how to access the AuthAction Management API using a Machine-to-Machine (M2M) application. Whether you’re building a backend service or a script that needs to manage users, roles, or other resources, this post will get you up and running with the correct setup and API calls.

An M2M application is perfect when your service or script needs to securely access APIs without any user interaction. This approach leverages the OAuth2 client credentials flow to obtain access tokens that can be used for authentication.

Step 1: Create an M2M Application in AuthAction

Section titled “Step 1: Create an M2M Application in AuthAction”
  1. Log in to your AuthAction dashboard.
  2. Navigate to the Applications section.
  3. Create a new application and select the type as Machine-to-Machine (M2M).
  4. In the application’s APIServer tab, ensure you grant access to the AuthAction Management API.

After creating the application:

  • Note down the Client ID and Client Secret for your M2M application.
  • These credentials will be essential for obtaining access tokens.

To access the Management API, you’ll need an access token via the client credentials flow. Here’s how you can do it:

POST https://<tenant>.<region>.authaction.com/oauth/m2m/token

Terminal window
curl --request POST \
--url https://<tenant>.<region>.authaction.com/oauth/m2m/token \
--header 'content-type: application/json' \
--data '{
"client_id": "<YOUR_M2M_APP_CLIENT_ID>",
"client_secret": "<YOUR_M2M_APP_CLIENT_SECRET>",
"audience": "https://<tenant>.<region>.authaction.com",
"grant_type": "client_credentials"
}'
{
"access_token": "YOUR_ACCESS_TOKEN",
"token_type": "Bearer",
"expires_in": 86400
}
  • The access_token will be used to authenticate requests to the Management API.
  • The token is valid for the duration specified in expires_in (e.g., 24 hours).

Once you have the access_token, you can use it to call any Management API endpoint (https://authaction.readme.io/).

Here’s how you can retrieve the list of users:

Terminal window
curl --request GET \
--url https://<tenant>.<region>.authaction.com/api/v1/users \
--header 'Authorization: Bearer YOUR_ACCESS_TOKEN'
[
{
"id": "671e6abcf34ff7b958a64f81",
"name": "John Doe",
"email": "john.doe@example.com"
},
...
]

If you encounter a 401 Unauthorized error:

  • Verify permissions: Ensure your M2M application has authorized to the AuthAction Management API.
  • Check the audience: Ensure the audience in your token request matches your AuthAction domain (e.g., https://<tenant>.<region>.authaction.com).

  • M2M applications allow backend services to interact with APIs securely, using access tokens obtained via the client credentials flow.
  • Properly configure the AuthAction Management API permissions in your M2M application.
  • Always validate the audience and other token parameters when calling APIs.

If you’re working with scripts or automation tools, you can store the Client ID and Client Secret in environment variables. Here’s a quick Node.js snippet for retrieving the token:

const axios = require("axios");
async function getAccessToken() {
const response = await axios.post(
"https://<tenant>.<region>.authaction.com/oauth/m2m/token",
{
client_id: process.env.M2M_CLIENT_ID,
client_secret: process.env.M2M_CLIENT_SECRET,
audience: `https://<tenant>.<region>.authaction.com`,
grant_type: "client_credentials",
}
);
return response.data.access_token;
}

Now you can securely manage and automate API requests with ease!