Last active
May 11, 2017 17:27
-
-
Save pitbulk/692f70512e49a7c0fcc6bfda6f655d0c to your computer and use it in GitHub Desktop.
Onelogin - OAuth 2.0 Tokens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// URL & credentials | |
$url = "https://api.<us or eu>.onelogin.com/auth/oauth2/token"; | |
$client_id = "<client_id>"; | |
$client_secret = "<client_secret>"; | |
$data = array( | |
"grant_type" => "client_credentials" | |
); | |
$data_string = json_encode($data); | |
$authorization = "client_id:$client_id, client_secret:$client_secret"; | |
$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; | |
$errorMsg .= " || ". $result_data->status->message; | |
throw new Exception($errorMsg); | |
} else { | |
$data = $result_data->data[0]; | |
$access_token = $data->access_token; | |
$created_at = $data->created_at; | |
$expires_in = $data->expires_in; | |
$refresh_token = $data->refresh_token; | |
$token_type = $data->token_type; | |
$account_id = $data->account_id; | |
} | |
} else { | |
throw new Exception(curl_error($ch), curl_errno($ch)); | |
} | |
curl_close($ch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// URL & credentials | |
$url = "https://api.<us or eu>.onelogin.com/auth/oauth2/token"; | |
// Parameters | |
$access_token = "<access_token>"; | |
$refresh_token = "<refresh_token>"; | |
$data = array( | |
"grant_type" => "refresh_token", | |
"access_token" => $access_token, | |
"refresh_token" => $refresh_token, | |
); | |
$data_string = json_encode($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', | |
'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; | |
$errorMsg .= " || ". $result_data->status->message; | |
throw new Exception($errorMsg); | |
} else { | |
$data = $result_data->data[0]; | |
$access_token = $data->access_token; | |
$created_at = $data->created_at; | |
$expires_in = $data->expires_in; | |
$refresh_token = $data->refresh_token; | |
$token_type = $data->token_type; | |
$account_id = $data->account_id; | |
} | |
} else { | |
throw new Exception(curl_error($ch), curl_errno($ch)); | |
} | |
curl_close($ch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// URL & credentials | |
$url = "https://api.<us or eu>.onelogin.com/auth/oauth2/revoke"; | |
$client_id = "<client_id>"; | |
$client_secret = "<client_secret>"; | |
// Parameters | |
$access_token = "<access_token>"; | |
$data = array( | |
"access_token" => $access_token | |
); | |
$data_string = json_encode($data); | |
$authorization = "client_id:$client_id, client_secret:$client_secret"; | |
$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; | |
$errorMsg .= " || ". $result_data->status->message; | |
throw new Exception($errorMsg); | |
} | |
} else { | |
throw new Exception(curl_error($ch), curl_errno($ch)); | |
} | |
curl_close($ch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// URL & credentials | |
$url = "https://api.<us or eu>.onelogin.com/auth/rate_limit"; | |
// Parameters | |
$access_token = "<access_token>"; | |
$authorization = "bearer:$access_token"; | |
$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( | |
'Content-Type:application/json', | |
'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 { | |
$data = (array)$result_data->data; | |
$limit = $data['X-RateLimit-Limit']; | |
$remaining = $data['X-RateLimit-Remaining']; | |
$reset = $data['X-RateLimit-Reset']; | |
echo $limit."<br>".$remaining."<br>".$reset; | |
} | |
} 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
In the refresh example, you don't need to include the client ID & secret. This should work with just access & refresh