Page 1 of 1

Calling pmRestLogin() from a Web Page

Posted: Tue Jun 11, 2019 1:45 pm
by mohamad
Dear Amos,
I can call REST endpoints of processmaker for itself without any problem.( Existing Access Token Inside ProcessMaker ). However, there are some problems when I want to call REST endpoints for external applications. I explain whatever I did:
1) I registered the external application, and PM gave me Client ID and Client Secret.
2) For calling pmRestLogin() from a Web Page, I used the following code and copied them to loginForm.php. (the loginForm.php has been placed in my wamp: localhost/example/loginForm.php)
Code: Select all
<html>
<body>
<?php //print error message if any from previous login attempt:
if (isset($_GET['error'])) 
  print "<p><font color=red>{$_GET['error']}</font></p>\n";
?> 
<form action="login.php" method="post">
  Client ID<br>
  <input type="text" name="client_id" value="" width=32 /><br>
  Client Secret<br>
  <input type="text" name="client_secret" value="" width=32 /><br>
  Username<br>
  <input type="text" name="username" value="" width=20 /><br>
  Password<br>
  <input type="text" name="password" value="" width=20 /><br>
  <input type="submit" value="Login"/>
</form>
</body>
</html>
3) In the login.php file, placed following code to call the pmRestLogin() function ( There are some syntax errors in https://wiki.processmaker.com/3.1/OAuth ... a_web_page, and you can correct them, if you find some time)
Code: Select all
<?php
$error = ""; 
if (!isset($_POST['client_id'])){
   $error = "No credentials POSTed to access ProcessMaker REST.";
} elseif ($_POST['client_id'] == "" or $_POST['client_secret'] == "") {
   $error = "No client ID or secret specified to get authorization from ProcessMaker OAuth.";
}elseif ($_POST['username'] == "" or $_POST['password'] == ""){
   $error = "No username or password specified to login to ProcessMaker.";
}
 
if ($error) {
   header('location: loginForm.php?error=' . urlencode($error)); //pass error so login screen can display it to user. 
   die();
}
 
$oToken = pmRestLogin($_POST['client_id'], $_POST['client_secret'], $_POST['username'], $_POST['password']);
if (isset($oToken) and isset($oToken->access_token)) {
   //can now call REST endpoints using $oToken->access_token
 $oRet = pmRestRequest("POST", "/api/1.0/workflow/group", null, $oToken->access_token);   
$apiServer = "http://localhost:67"; 
$postParams = array(
   'grp_title'   => "My group",
   'grp_status'  => "ACTIVE"
);
$ch = curl_init($apiServer . "/api/1.0/workflow/group");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $oToken "));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$oGroup = json_decode(curl_exec($ch));

if (!isset($oGroup)) {
   print "Error accessing $apiServer: \n" . curl_error($ch);
}
elseif (isset($oGroup->error)) {
   print "Error in $apiServer: \nCode: {$oGroup->error->code}\nMessage: {$oGroup->error->message}\n";
}
else {
   print "Group '{$oGroup->grp_title}' created with UID: {$oGroup->grp_uid}\n";
}
curl_close($ch);

}
4) When I am going to run my wamp address (localhost/example/loginForm.php), after entering Client Id, client Secret, username and password, and submitting the form, I see this fatal error: Call to undefined function pmRestLogin() in D:\wamp64\www\example\login.php on line 16
I would be very grateful if you could help me to solve it.
Best Regards

Re: Calling pmRestLogin() from a Web Page

Posted: Tue Jun 11, 2019 8:19 pm
by amosbatto
At the top of your PHP file, change from:
Code: Select all
<?php
$error = ""; 
To:
Code: Select all
<?php
include "/path/to/my/rest/functions/file.php"; //set to path on server (not the web URL).
$error = ""; 

Can you list the exact syntax errors that you found in https://wiki.processmaker.com/3.1/OAuth ... a_web_page ?

Re: Calling pmRestLogin() from a Web Page

Posted: Tue Jun 11, 2019 8:35 pm
by amosbatto
What I mean is that you need to create a PHP file on your ProcessMaker server which defines the pmLogin and pmRestRequest functions as shown here:
https://wiki.processmaker.com/3.0/Calli ... PHP_plugin

Let's say that you store that file at the location: /opt/extra/restFunctions.php

Then, the commend to import it into another PHP file would be:
include "/opt/extra/restFunctions.php";

Re: Calling pmRestLogin() from a Web Page

Posted: Wed Jun 12, 2019 8:29 am
by mohamad
amosbatto wrote: Tue Jun 11, 2019 8:19 pm Can you list the exact syntax errors that you found in https://wiki.processmaker.com/3.1/OAuth ... a_web_page ?
Sure, of course these are just syntax errors, as far as I know.
At line 6, change from:
Code: Select all
$error = "No client ID or secret specified to get authorization from ProcessMaker OAuth.");
To:
Code: Select all
$error = "No client ID or secret specified to get authorization from ProcessMaker OAuth.";
At line 8, change from:
Code: Select all
$error = "No username or password specified to login to ProcessMaker.");
To:
Code: Select all
$error = "No username or password specified to login to ProcessMaker.";
At line 15, change from:
Code: Select all
$oToken = pmRestLogin($_POST['client_id', $_POST['client_secret'], $_POST['username'], $_POST['password']);
To:
Code: Select all
$oToken = pmRestLogin($_POST['client_id'], $_POST['client_secret'], $_POST['username'], $_POST['password']);
Thanks, you've been very helpful. Your solutions are great.

One more question, I searched the wiki to find some examples about calling REST endpoints of other applications in PM, but I could not find. I mean, if I want to get APIs' another software to call them in PM,how can I do that? (By iframe, plugin or ...). So, the first thing I need to do is that, I should have some information about another application?
What about desktop application?According to wiki, ProcessMaker functionality can be reimplemented in desktop applications,too. Is there any example?
Would you please help me? I would be appreciated if you could send some links.
Kind Regards

Re: Calling pmRestLogin() from a Web Page

Posted: Wed Jun 12, 2019 11:17 pm
by amosbatto
mohamad wrote: Wed Jun 12, 2019 8:29 am
amosbatto wrote: Tue Jun 11, 2019 8:19 pm Can you list the exact syntax errors that you found in https://wiki.processmaker.com/3.1/OAuth ... a_web_page ?
Sure, of course these are just syntax errors, as far as I know.
Yes, they are errors. I fixed them in the documentation. I can't believe that nobody spotted them before you.
mohamad wrote:One more question, I searched the wiki to find some examples about calling REST endpoints of other applications in PM, but I could not find. I mean, if I want to get APIs' another software to call them in PM,how can I do that? (By iframe, plugin or ...). So, the first thing I need to do is that, I should have some information about another application?
What about desktop application?According to wiki, ProcessMaker functionality can be reimplemented in desktop applications,too. Is there any example?
If you want to call another application's REST endpoints when inside a ProcessMaker trigger or Dynaform, the code is basically the same as calling ProcessMaker's REST endpoints. You need to login to the other service and get its access token. If they use oAuth2 like ProcessMaker for authentication, then it should be the same code as logging into ProcessMaker's oAuth2 service, only with a different URL. Then, calling their REST endpoints should be the same code. See these examples:
https://wiki.processmaker.com/3.0/Calli ... points#PHP
https://wiki.processmaker.com/3.2/JavaS ... ing_jQuery

I don't have any examples using REST in a desktop application, but I find many examples by Googling "REST application [operating_system] [programming_language]". Since I don't know what operating system and what programming languages you use, it is better if you do the search.

Re: Calling pmRestLogin() from a Web Page

Posted: Thu Jun 13, 2019 2:42 am
by mohamad
Thank you very much