API Example User Authentication
From InClickAdServer
User Authentication
This API Service Request will authenticate the username and password pair for login.
In the factory User Interface (UI), the corresponding action is when someone logs into their account. On success, this service request will provide the User ID (Account Number), the User Name, the users first and last name, and the account type.
The Code Example
<?php require_once "../globals/globals.php"; require_once (INCLICK_CORE_DIR . "/external/classes/users/IMG_UserManagerClient.php"); //THE BASE URL OF YOUR AD SERVER DEPLOYMENT $base_url = INCLICK_WEB_URL; //THE SERVICE KEY TO ACCESS THE API $service_key = "YOUR_API_SERVICE_KEY"; //INSTANTIATE THE API CLIENT $user_manager = new IMG_UserManagerClient($base_url, $service_key); $result = $user_manager->validateUserLogin("$username", "$password"); $validation_result = $result->getReturnValue(); $user_account = $validation_result->getUserAccount(); if ($validation_result->getValidated() == 1) { } else { } ?>
Breaking Down the Code
require "../globals/globals.php";
This require references the local inClick Ad Server deployment configuration files allowing your code to utilize the pre-packaged API Objects as well as the bundled PHPOlait services.
require_once (INCLICK_CORE_DIR . "/external/classes/users/IMG_UserManagerClient.php");
This will include the files that contain the API Objects and Classes needed for this API Service Request.
$base_url = INCLICK_WEB_URL; $service_key = "YOUR_API_SERVICE_KEY";
The $base_url is the URL to your inClick Ad Server deployment, the ad server root URL. For local host API usage, the constant of INCLICK_WEB_URL is defined in the globals.php file referenced earlier. Remote deployments, deployments where the ad server and API Request Location are not on the same server, would change this to the fully qualified URL to the ad server root directory.
The $service_key is one of the the api_service_keys values you created for your ad server. (more information)
$user_manager = new IMG_UserManagerClient($base_url, $service_key);
This is where we create an instance of the classes needed to perform a User Authentication request.
$result = $user_manager->validateUserLogin("$username", "$password");
This line processes the API Service Request using the User Name ($username) and Password ($password) provided.
$validation_result = $result->getReturnValue(); $user_account = $validation_result->getUserAccount();
These two lines set the status objects:
| Object | Description | Response Content |
|---|---|---|
| $validation_result->getValidated() | Indicates if the the username and password pass or fail | null=failed 1=passed |
| $user_account->getUserID() | The account number of the authenticated user | integer |
| $user_account->getUsername() | The registered user name of the user | Text |
| $user_account->getFirstname() | The registered first name of the user | Text |
| $user_account->getLastname() | The registered last name of the user | Text |
| $user_account->getUserTypeCode() | The account type of the authenticated user | adv pub adm |
| $validation_result->getMessage() | Available on failures only, a verbose reason for the failed login | Text |
| $validation_result->getCode() | The reason code for the failure | authentication_failed account_locked |
if ($validation_result->getValidated() == 1) { echo ("User Authentication Passed!<br> "); echo ("User ID: " . $user_account->getUserID() . "<br>"); echo ("Username: " . $user_account->getUsername() . "<br>"); echo ("Name: " . $user_account->getFirstname() . " " . $user_account->getLastname() . "<br>"); echo ("Account Type: " . $user_account->getUserTypeCode() . "<br>"); echo ("Result Message: " . $validation_result->getMessage() . "<br>"); } else { echo ("User Authentication Failed!<br>"); echo ("Result Code: " . $validation_result->getCode() . "<br>"); echo ("Result Message: " . $validation_result->getMessage() . "<br>"); }
This section of code is provided to give you a visual confirmation of what occurred, or to let you know what error occurred.
