PHP code example of aslaluroba / babylai_php_auth_client

1. Go to this page and download the library: Download aslaluroba/babylai_php_auth_client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aslaluroba / babylai_php_auth_client example snippets



BabylAI\Client\BabylAiAuthClient;

$tenantId = '#####';  // replace with your TenantId
$apiKey   = '#####';  // replace with your API key

$client = new BabylAiAuthClient('https://babylai.net/api/');

try {
    $resp = $client->getClientToken($tenantId, $apiKey);
    echo "Token: " . $resp->getToken() . PHP_EOL;
    echo "Expires in: " . $resp->getExpiresIn() . " seconds" . PHP_EOL;
} catch (\Exception $ex) {
    echo "Error: " . $ex->getMessage() . PHP_EOL;
}

   
   return [
       'tenant_id' => env('BABYLAI_TENANT_ID', ''),
       'api_key'   => env('BABYLAI_API_KEY', ''),
       'base_url'  => env('BABYLAI_BASE_URL', 'https://babylai.net/api/'),
   ];
   

   use BabylAI\Client\BabylAiAuthClient;
   use GuzzleHttp\Client as GuzzleClient;

   public function register(): void
   {
       $this->app->singleton(BabylAiAuthClient::class, function ($app) {
           $cfg = $app->config->get('babylai');
           $tenantId = $cfg['tenant_id'];
           $apiKey   = $cfg['api_key'];
           $baseUrl  = $cfg['base_url'];

           $guzzle = new GuzzleClient([
               'base_uri' => rtrim($baseUrl, '/') . '/'
           ]);

           return new BabylAiAuthClient($baseUrl, $guzzle);
       });
   }
   

   

   namespace App\Http\Controllers\Api;

   use App\Http\Controllers\Controller;
   use BabylAI\Client\BabylAiAuthClient;
   use Illuminate\Http\JsonResponse;
   use Illuminate\Support\Facades\Log;
   use Exception;

   class TokenController extends Controller
   {
       private BabylAiAuthClient $authClient;

       public function __construct(BabylAiAuthClient $authClient)
       {
           $this->authClient = $authClient;
       }

       public function getClientToken(): JsonResponse
       {
           try {
               $cfg = config('babylai');
               $tenantId = (string) $cfg['tenant_id'];
               $apiKey   = (string) $cfg['api_key'];

               $resp = $this->authClient->getClientToken($tenantId, $apiKey);

               return response()->json([
                   'token'     => $resp->getToken(),
                   'expiresIn' => $resp->getExpiresIn(),
               ], 200);
           } catch (Exception $ex) {
               Log::error('BabylAI TokenController: ' . $ex->getMessage());
               $prev = $ex->getPrevious();
               if ($prev && method_exists($prev, 'getResponse')) {
                   $body = (string) $prev->getResponse()->getBody();
                   return response()->json([
                       'error'   => 'Upstream error',
                       'details' => $body,
                   ], 502);
               }
               return response()->json([
                   'error' => $ex->getMessage()
               ], 500);
           }
       }
   }
   

   
   use Illuminate\Support\Facades\Route;
   use App\Http\Controllers\Api\TokenController;

   Route::get('Token', [TokenController::class, 'getClientToken']);
   

   
   return [
       'paths' => ['api/*'],
       'allowed_methods' => ['*'],
       'allowed_origins' => [
           'http://localhost:4200',
           'https://localhost:4200',
           'http://localhost:3000',
           'https://localhost:3000',
           'http://localhost:5173',
           'https://localhost:5173',
       ],
       'allowed_origins_patterns' => [],
       'allowed_headers' => ['*'],
       'exposed_headers' => [],
       'max_age' => 0,
       'supports_credentials' => false,
   ];
   
bash
   composer 
bash
php test.php
bash
   composer 
ini
   BABYLAI_TENANT_ID=2176c188-8cb4-4fc3-8366-2d32e601f7e5
   BABYLAI_API_KEY="I/22UK34I6xAhglfPXTwPAgUuVyJw8TdnG26ZLI5gYQ="
   BABYLAI_BASE_URL="https://babylai.net/api/"
   
bash
   php artisan config:clear
   
bash
   php artisan make:controller Api/TokenController
   
bash
   php artisan route:clear
   php artisan route:list
   
bash
   php artisan config:clear