Use the API to log in and redirect to a page
Convert an API-authenticated user into a web-authenticated user and send them to a target intranet page using a single-use token.
Before you start
- You need the API domain and tenant GUID for your instance. See How to get API information.
Overview
You can use the API to log a user into their intranet and navigate them to a page, to support integrations with third-party systems. The single-use token and redirector page convert an API-authenticated user into a web-authenticated user without going through the login process again.
The example PHP code below logs the user into the API, creates a single-use token for the redirector page, then navigates the user to the target page on the intranet.
The script
<?
// Get variables for API calls
$apiDomain = "YOUR API DOMAIN";
$webDomain = "YOUR WEB DOMAIN";
$tenantGuid = "YOUR TENANT GUID";
$pageUrl = "YOUR TARGET PAGE";
$user = "USERNAME";
$pw = "PASSWORD";
$accessToken = "";
// login to the API
$resp = callApi("/token", "username=".$user."&password=".$pw."&grant_type=password");
$accessToken = $resp['access_token'];
// get a one time use token
$resp = callApi("/api/logintoken","");
$oneTimeToken = $resp['login_token'];
// redirect to redirector
$returnUrl = $webDomain.$pageUrl;
$url = $webDomain."/redirector?token=".$oneTimeToken."&returnUrl=".urlencode($returnUrl);
header("Location: ".$url);
// helper functions
function callApi($url, $body){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $GLOBALS['apiDomain'].$url);
if($body != ""){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
'Content-type: application/x-www-form-urlencoded',
'X-Tenant: '.$GLOBALS['tenantGuid'],
'Authorization: Bearer '.$GLOBALS['accessToken']
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec($ch);
curl_close($ch);
return json_decode($server_output, true);
}
?>
How it works
callApi("/token", ...)logs the user into the API and returns an access token.callApi("/api/logintoken", "")requests a single-use login token.- The script builds a redirector URL with the login token and the target return URL, then redirects the browser to it.
Related
Section: REST API