- Thu Nov 03, 2016 5:47 pm
#787303
Hello forum
I am trying to build an adapter that will connect to external applications and run the workflow defined in the process manager.
Reading the manual I understood I need to do the steps:
1. Get Aoauth2 token
2. Use the token to call the REST API.
Although it looks simple, I cannot make it work and I do not know if this is problem in my program or something. There is always an error that "Grant type not defined"
Below is the program I wrote, which is simple the function as in the documentation.
I would like your advise.
Thanks a lot!
I am trying to build an adapter that will connect to external applications and run the workflow defined in the process manager.
Reading the manual I understood I need to do the steps:
1. Get Aoauth2 token
2. Use the token to call the REST API.
Although it looks simple, I cannot make it work and I do not know if this is problem in my program or something. There is always an error that "Grant type not defined"
Below is the program I wrote, which is simple the function as in the documentation.
I would like your advise.
Thanks a lot!
Code: Select all
<?php
$pmServer = 'http://pm.convergence.solutions';
$pmWorkspace = 'workflow';
$json = file_get_contents("oauthCredentials.json") or
die("Error: Unable to open file oauthCredentials.json.");
$oCred = json_decode($json);
$oToken = pmRestLogin($oCred->client_id, $oCred->client_secret, $oCred->username, $oCred->password);
if (isset($oToken) and isset($oToken->access_token)) {
//can now call REST endpoints using $oToken->access_token
$oRet = pmRestRequest("GET", "/api/1.0/workflow/users", null, $oToken->access_token);
}
function pmRestLogin($clientId, $clientSecret, $username, $password) {
global $pmServer, $pmWorkspace;
$postParams = array(
'grant_type' => 'password',
'scope' => '*', //set to 'view_process' if not changing the process
'client_id' => $clientId,
'client_secret' => $clientSecret,
'username' => $username,
'password' => $password
);
$ch = curl_init("$pmServer/$pmWorkspace/oauth2/token");
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postParams));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$oToken = json_decode(curl_exec($ch));
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpStatus != 200) {
print "Error in HTTP status code: $httpStatus\n";
return null;
}
elseif (isset($oToken->error)) {
print "Error logging into $pmServer:\n" .
"Error: {$oToken->error}\n" .
"Description: {$oToken->error_description}\n";
}
else {
//At this point $oToken->access_token can be used to call REST endpoints.
//If planning to use the access_token later, either save the access_token
//and refresh_token as cookies or save them to a file in a secure location.
//If saving them as cookies:
//setcookie("access_token", $oToken->access_token, time() + 86400);
//setcookie("refresh_token", $oToken->refresh_token); //refresh token doesn't expire
//setcookie("client_id", $clientId);
//setcookie("client_secret", $clientSecret);
//If saving to a file:
file_put_contents("oauthAccess.json", json_encode($tokenData));
}
return $oToken;
}
?>