- Mon Jun 27, 2022 2:15 pm
#830021
Hello,
Processmaker has been running on our server for a couple years now without any issues. All of the sudden today my API script is failing during the pmRestLogin function with an HTTP status code of 0.
As far as I am aware nothing has changed. The client ID, secret, username and password are stored in a file called oauthAccess.json as described in the wiki. The function is the exact same as the wiki as well.
I run the authentication function and store the token in a variable to be called later for my REST api script. any idea as to why I am getting an HTTP status code of 0?
Processmaker has been running on our server for a couple years now without any issues. All of the sudden today my API script is failing during the pmRestLogin function with an HTTP status code of 0.
As far as I am aware nothing has changed. The client ID, secret, username and password are stored in a file called oauthAccess.json as described in the wiki. The function is the exact same as the wiki as well.
I run the authentication function and store the token in a variable to be called later for my REST api script. any idea as to why I am getting an HTTP status code of 0?
Code: Select all
$pmServer = 'serverAddress';
$pmWorkspace = 'workspace';
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, $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("/location/oauthAccess.json", json_encode($oToken));
}
return $oToken;
}
//--------------------------------------------------------------------------
//include the path in the filename if not located in the same directory:
$json = file_get_contents("oauthCredentials.json") or
die("Error: Unable to open file oauthCredentials.json.");
$oCred = json_decode($json);
if (empty($oCred) or !isset($oCred->client_id)){
die("Error: oauthCredentials.json file lacks credentials or JSON string can't be decoded.");
}
$oToken = pmRestLogin($oCred->client_id, $oCred->client_secret, $oCred->username, $oCred->password);
$accessToken = $oToken->access_token;