API - Authentication API
Posted: Sun Jun 15, 2025 8:19 am
Hi,
I have been following the API documentation. Now there are two sections for authentication. The first talks about
1. Authentication API - For Restful API, use key and secret, and develop X-authorization. I changed API settings to choose the secret and key auth method in the admin section for this.
2. The second section (in documentation) talks of /api/auth/authenticate using username and password.
My focus is in using the key and secret (key and secret). However, when I use the code below, it keeps telling me that "token could not be established" [not the exact phrase]. However, when I removed X-authorization (meaning the $codice and X-authorization from the code) and in the formalms admin setting chose token-based auth method. It worked, returning me a "success" and also a token.
But I don't intend to use token-based authorization.
And later, as I tinkered with the code, I realized that in the code (API folder), the code doesn't tell what needs to be done if the auth method is secret & Key, but does tell what should be done if the auth method is token type (i.e. generate token). [Not sure if I missed anything in the code that actually directs what needs to be done for key & secret based auth code]
What I want to do is I want to authenticate using key and secret using the X-authorizatoin as discussed in the documentation. Please help.
The code I used is below, which does not give me success for key and secret based authorization (but provides me token and success if removed the section on "Generating auth code" and X-authorizatoin .
<?php
// Only handle POST if form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['username'], $_POST['password'])) {
// Basic input sanitization
$user = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$pass = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
// Replace with your actual API credentials
$key = 'key';
$secret = 'key_secret';
$api_url = 'https://mydomain.com/api/rest.php?q=/ap ... thenticate';
// Generate auth code
$params = [$user, $pass];
$sha1_hash = strtolower(sha1(implode(',', $params) . ',' . $secret));
$auth_code = base64_encode($key . ':' . $sha1_hash);
// Build POST request with cURL
$post_fields = http_build_query([
'username' => $user,
'password' => $pass
]);
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Accept: application/xml",
"Content-Type: application/x-www-form-urlencoded",
"X-Authorization: FormaLMS $auth_code"
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
curl_close($ch);
// Display results
I have been following the API documentation. Now there are two sections for authentication. The first talks about
1. Authentication API - For Restful API, use key and secret, and develop X-authorization. I changed API settings to choose the secret and key auth method in the admin section for this.
2. The second section (in documentation) talks of /api/auth/authenticate using username and password.
My focus is in using the key and secret (key and secret). However, when I use the code below, it keeps telling me that "token could not be established" [not the exact phrase]. However, when I removed X-authorization (meaning the $codice and X-authorization from the code) and in the formalms admin setting chose token-based auth method. It worked, returning me a "success" and also a token.
But I don't intend to use token-based authorization.
And later, as I tinkered with the code, I realized that in the code (API folder), the code doesn't tell what needs to be done if the auth method is secret & Key, but does tell what should be done if the auth method is token type (i.e. generate token). [Not sure if I missed anything in the code that actually directs what needs to be done for key & secret based auth code]
What I want to do is I want to authenticate using key and secret using the X-authorizatoin as discussed in the documentation. Please help.
The code I used is below, which does not give me success for key and secret based authorization (but provides me token and success if removed the section on "Generating auth code" and X-authorizatoin .
<?php
// Only handle POST if form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['username'], $_POST['password'])) {
// Basic input sanitization
$user = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$pass = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
// Replace with your actual API credentials
$key = 'key';
$secret = 'key_secret';
$api_url = 'https://mydomain.com/api/rest.php?q=/ap ... thenticate';
// Generate auth code
$params = [$user, $pass];
$sha1_hash = strtolower(sha1(implode(',', $params) . ',' . $secret));
$auth_code = base64_encode($key . ':' . $sha1_hash);
// Build POST request with cURL
$post_fields = http_build_query([
'username' => $user,
'password' => $pass
]);
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Accept: application/xml",
"Content-Type: application/x-www-form-urlencoded",
"X-Authorization: FormaLMS $auth_code"
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
curl_close($ch);
// Display results