Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
By gnikoloudis
#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!
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; 
}
?>
User avatar
By amosbatto
#787305
You are running this as an external script (not in a trigger in ProcessMaker)?

First of all, make sure the URL is correct by placing it in the address bar of your web browser:
http://pm.convergence.solutions/workflow/oauth2/token
You should see this in the browser window:
Code: Select all
{"error":{"code":405,"message":"Method Not Allowed"}}
I assume from the error message that your POST variables aren't being sent correctly. I see that you are pulling the variables from a file. Stick var_dump() in your file to verify that the variables are being set correctly:
Code: Select all
$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
   );
var_dump($postParams);
var_dump(json_encode($postParams));

What version of PHP are you using?
By gnikoloudis
#787336
Hi
Thanks a lot for your feedback.

Taken one by one the points:

Yes I am sending this from external script.

The URL is correct according to the sample on site. Is there anything else to consider for the URL address

The dump of the posts are:

array(6) {
["grant_type"]=>
string(8) "password"
["scope"]=>
string(1) "*"
["client_id"]=>
string(32) "PUZCQCKTXCFMZYOBBLEHNXDEFMRYKITZ"
["client_secret"]=>
string(32) "106011167581b3baa433199031521577"
["username"]=>
string(5) "admin"
["password"]=>
string(15) "<password>" ( I am not sending the password for security reasons)
}
string(183) "{"grant_type":"password","scope":"*","client_id":"PUZCQCKTXCFMZYOBBLEHNXDEFMRYKITZ","client_secret":"106011167581b3baa433199031521577","username":" "admin","password":"<password>"}"


my php version is :

PHP 5.6.22-0+deb8u1 (cli) (built: Jun 9 2016 07:14:06)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies


Any idea how to proceed?

Thanks

George
User avatar
By amosbatto
#787360
The information is correct that you are sending and the fact that you are getting that error message means that you have the right URL. It should work. What version of PM are you using and what is your operating system. I want to try to replicate this. Is this a custom install or did you use the automatic ProcessMaker installer?

Have you tried using Postman in Chrome or Firefox to send the JSON string to the URL? If not, try it with your JSON string:
Code: Select all
{"grant_type":"password","scope":"*","client_id":"PUZCQCKTXCFMZYOBBLEHNXDEFMRYKITZ","client_secret":"106011167581b3baa433199031521577","username":" "admin","password":"<password>"}
Most Demanding OST to PST Converter

If you are looking for the most demanding and one […]

Interested in launching your NFT marketplace platf[…]

When you buy Klonopin online from Xanaxgeneric.com[…]

Are you looking to buy Xanax online? Xanax is a me[…]