Skip to content

Instantly share code, notes, and snippets.

@pitbulk
Last active March 27, 2017 21:44
Show Gist options
  • Save pitbulk/60a4c74916e4882e50464181854aa6dc to your computer and use it in GitHub Desktop.
Save pitbulk/60a4c74916e4882e50464181854aa6dc to your computer and use it in GitHub Desktop.
Onelogin - Events API
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/events";
$access_token = "<access_token>";
// Parameters
$query_parameters = array(
// -- Search --
'client_id' => '',
'created_at' => '',
'directory_id' => '',
'event_type_id' => '',
'id' => '',
'resolution' => '',
'since' => '',
'until' => '',
'user_id' => '',
// -- Pagination --
// 'after_cursor' => '',
// 'before_cursor' => '',
// -- Limit --
'limit' => 50, // Max limit (default value)
// -- Sort --
// 'sort' => '+id',
// -- Fields (return only those fields) --
// 'fields' => 'created_at, event_type_id, user_id'
// -- Since and Until (UTC string value) --
'since' => null,
'until' => null,
);
$query = http_build_query(array_filter($query_parameters));
$authorization = "bearer:$access_token";
if (!empty($query)) {
$url .= '?' . $query;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Authorization:'.$authorization
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
} else if (empty($result_data->data)) {
// No result
$events = array();
} else {
$events = $result_data->data;
$before_cursor = $result_data->pagination->before_cursor;
$after_cursor = $result_data->pagination->after_cursor;
// Now if $after_cursor is not empty, I can pass it as
// a parameter of $query_parameters and execute again
// the curl to retrieve all events.
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
// Let's review the parameters used on the different examples listed at
// https://developers.onelogin.com/api-docs/1/events/get-events
// Get Events Created within Date/Time Range
/*
$query_parameters = array(
'since' => '<since>',
'until' => '<until>'
);
*/
// Get Events by Event Type ID
/*
$query_parameters = array(
'event_type_id' => '<event_type_id>'
);
*/
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/events";
$access_token = "<access_token>";
// Parameters
$id = "<event_id>";
$authorization = "bearer:$access_token";
if (empty($id)) {
throw new Exception("id parameter can't be empty. If you don’t know the user’s id, use the Get Users API call");
}
$url .= "/".$id;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Authorization:'.$authorization
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
$errorMsg .= " || ". $result_data->status->message;
throw new Exception($errorMsg);
} else {
$event = $result_data->data;
print_r($event);
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
<?php
// URL & credentials
$url = "https://api.<us or eu>.onelogin.com/api/1/events";
$access_token = "<access_token>";
// Parameters (event data)
$event_data = array (
# Required
"event_type_id" => (int) "<event_type_id>",
# Required
"account_id" => (int) "<account_id>",
/*
"actor_system" => "",
"actor_user_id" => "",
"actor_user_name" => "",
"app_id" => "",
"assuming_acting_user_id" => "",
"custom_message" => "",
"directory_sync_run_id" => "",
"group_id" => "",
"group_name" => "",
"ipaddr" => "",
"otp_device_id" => "",
"otp_device_name" => "",
"policy_id" => "",
"policy_name" => "",
"role_id" => "",
"role_name" => "",
"user_id" => "",
"user_name" => ""
*/
);
$authorization = "bearer:$access_token";
$data_string = json_encode($event_data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$header_opts = array(
'Content-Type:application/json',
'Authorization:'.$authorization,
'Content-Length: ' . strlen($data_string)
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_opts);
$result = curl_exec($ch);
if ($result !== false) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $header_size);
$result_data = json_decode($body);
if ($result_data->status->error == true) {
$errorMsg = $result_data->status->code. ", ".$result_data->status->type;
if ($result_data->status->message instanceof stdClass) {
$errorMsg .= " || " . $result_data->status->message->description;
} else {
$errorMsg .= " || " . $result_data->status->message;
}
throw new Exception($errorMsg);
}
} else {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment