Problem with REST API Authentication

Any issue about Cyclos 4 scripting and Webservices

Moderators: hugo, alexandre, rmvanarkel

Post Reply
logzy
Posts: 4
Joined: Tue Dec 04, 2012 3:08 pm

Problem with REST API Authentication

Post by logzy » Wed May 03, 2017 12:29 pm

Hi all,

I have successfully used the API endpoints that do not require authentication for my cyclos installation, however, for those that require authentication i get missing "missingAuthorization" error.

Here is what i have tried to do

1) I use basic authentication by passing my admin credentials as username and password
2) I have created an access client and generated an activation code for a user but find it difficult to activate it via http[s]://cyclosuri/api/clients/activate

Please your urgent help is required on how to perform authentication for API calls.


Thanks

martin.rueegg
Posts: 80
Joined: Thu Aug 11, 2016 12:38 pm
Location: Bristol, UK
Contact:

Re: Problem with REST API Authentication

Post by martin.rueegg » Thu Sep 07, 2017 6:47 am

HI logzy

would be great if you could provide the code you're using as it is hard to tell what might go wrong.

Below you can see how I did it. PLEASE NOTE: it's just a proof of concept and there is no error handling implemented yet which is especially crucial for the blocking and unblocking of the tokens, as those functions do not return a result.

The library can be installed using our packagist/composer repo:

Code: Select all

composer require bristolpound/cyclos-php-library

Code: Select all

#!/bin/php
<?php

require 'vendor/autoload.php';

$channel = 'MyChannel';
$username = 'myUser';
$password = 'myPassword';
$pin = 1234;
$prefix = 'prefix_';

Cyclos\Configuration::setRootUrl("https://demo.cylos.org");
Cyclos\Configuration::setAuthentication($username, $password);
Cyclos\Configuration::setChannel($channel);

$acService = new Cyclos\AccessClientService();
$page = $acService->getTypeData('self', $channel);
foreach ($page as $acld)
{
	print_r($acld);
	Cyclos\Configuration::setAuthentication($username, $password);
	
	if (!$acld->canManage || !($acld->canCreateNew || $acld->hasUnassigned))
	{
		echo "NOT MANAGABLE\n";
		continue;
	}
	
	// list all current Access Clients of this type
	$ac = $acService->getListData($acld->type, array('username' => 'self'));
	print_r($ac);
	
	// get data for creating a new one
	$query = new stdclass();
	$query->user = 'self';
	$query->type = $acld->type->internalName;
	$ac = $acService->getDataForNew($query);
	print_r($ac);
	
	// create a new Access Client
	$dto = $ac->dto;
	$dto->name = "test".time();
	$id = $acService->save($dto);
	echo "$id\n";
	
	// get activation code
	$ac = $acService->load($id);
	print_r($ac);
	$code = $acService->getActivationCode(array('accessClient' => $id, 'confirmationPassword' => $pin));
	echo "\nActivation Code: $code\n\n";
	
	// activate Acces Client (get access token)
	$dto = $acService->activate($code, $prefix);
	print_r($dto);

	// now use the new access token for authenication
	Cyclos\Configuration::setAccessClientToken($prefix.$dto->token);

	// do something with it
	$userService = new Cyclos\UserService();
	$query = new stdclass();
	$query->keywords = 'C*';
	$query->pageSize = 1;
	$query->ignoreProfileFieldsInList = true;
	$page = $userService->search($query);

	echo("Found a total of $page->totalCount users\n");

	if (!empty($page->pageItems)) {
		foreach ($page->pageItems as $user) {
			echo("* $user->display ($user->shortDisplay)\n");	
		}
	}

	// block the access token
	$acService->block(array('accessClient' => $id, 'confirmationPassword' => $pin));
	echo "blocked.\n";
	
	// unblock it again
	Cyclos\Configuration::setAuthentication($username, $password);
	$acService->unblock(array('accessClient' => $id, 'confirmationPassword' => $pin));
	echo "unblocked\n";
	
	Cyclos\Configuration::setAccessClientToken('prefix_'.$dto->token);

	// and finally unassign it
	$result = $acService->unassignCurrent(array('confirmationPassword' => $pin));
	print_r($result);
	echo "\nunassigned\n";
}
Cyclos v4.8.2 w/ Social license
Technical Manager, Bristol Pound CIC, Bristol, UK, http://bristolpound.org

hafeez
Posts: 2
Joined: Wed Sep 13, 2017 9:17 pm

Re: Problem with REST API Authentication

Post by hafeez » Wed Sep 27, 2017 8:34 am

Hi All,

i am getting error same like that.. here is my code
file configureCyclos.php
<?php
function load($c) {
if (strpos($c, "Cyclos\\") >= 0) {
include str_replace("\\", "/", $c) . ".php";
}
}
spl_autoload_register('load');

Cyclos\Configuration::setRootUrl("https://xxx.com");
Cyclos\Configuration::setAuthentication("username", "xxxxx");

?>

now next i am doing like that
<?php
require_once 'configureCyclos.php';
$adService = new Cyclos\AdService();


/*echo '<pre>';
print_r($adService);
exit;*/

$query = new stdclass();
$query->keywords = 'Computer*';
$query->pageSize = 10;
$query->orderBy = 'PRICE_LOWEST';
$page = $adService->search($query);

echo '<pre>';
print_r($page);
exit;

echo("Found a total of $page->totalCount advertisements\n");
if (!empty($page->pageItems)) {
foreach ($page->pageItems as $ad) {
echo("* $ad->name\n");
}
}
?>

when i run the code i get this error
Fatal error: Uncaught exception 'Cyclos\ServiceException' with message 'Error calling Cyclos service: productsAndServices.search: UNKNOWN' in /home/khitomer1/public_html/cyclos/Cyclos/Service.php:36 Stack trace: #0 /home/khitomer1/public_html/cyclos/Cyclos/AdService.php(111): Cyclos\Service->__run('search', Array) #1 /home/khitomer1/public_html/cyclos/test.php(14): Cyclos\AdService->search(Object(stdClass)) #2 {main} thrown in /home/khitomer1/public_html/cyclos/Cyclos/Service.php on line 36

please help me as i am new to Cyclos
Attachments
exception.png
exception.png (20.43 KiB) Viewed 1098 times
Last edited by hafeez on Mon Oct 02, 2017 10:47 am, edited 1 time in total.

martin.rueegg
Posts: 80
Joined: Thu Aug 11, 2016 12:38 pm
Location: Bristol, UK
Contact:

Re: Problem with REST API Authentication

Post by martin.rueegg » Mon Oct 02, 2017 10:33 am

hafeez wrote:Hi All,
Cyclos\Configuration::setRootUrl("https://bank.ppayinc.com");
Cyclos\Configuration::setAuthentication("Dpwebb", "Cookies99");
Hi David,

Never add your actual credentials on a public post. Not even in a forum that requires access. I've logged in and changed the password and informed you by e-mail.

Thanks,
Martin.
Cyclos v4.8.2 w/ Social license
Technical Manager, Bristol Pound CIC, Bristol, UK, http://bristolpound.org

martin.rueegg
Posts: 80
Joined: Thu Aug 11, 2016 12:38 pm
Location: Bristol, UK
Contact:

Re: Problem with REST API Authentication

Post by martin.rueegg » Mon Oct 02, 2017 10:49 am

Hi David,
hafeez wrote:i am getting error same like that..
No, actually, it is not the same error and a completely unrelated question. The original question is about authentication, not about an UNKNOWN error when using the API.

Furthermore it would be helpful if you could put your code in

Code: Select all

[code]
[/code] tags, so it is formatted correctly and readable. Same for error messages.

Please create a new forum entry for your problem and try to apply a better stile of posting your question and please avoid posting your life credentials!

Just one note re your question: AdService has no search method ...
hafeez wrote:

Code: Select all

$adService = new Cyclos\AdService();
$query = new stdclass();
$query->keywords = 'Computer*';
$page = $adService->search($query);
Martin.
Cyclos v4.8.2 w/ Social license
Technical Manager, Bristol Pound CIC, Bristol, UK, http://bristolpound.org

martin.rueegg
Posts: 80
Joined: Thu Aug 11, 2016 12:38 pm
Location: Bristol, UK
Contact:

Re: Problem with REST API Authentication

Post by martin.rueegg » Mon Oct 02, 2017 10:53 am

Apologies, the following is obviously nonsense:
martin.rueegg wrote: Just one note re your question: AdService has no search method ...
The method exists!

Please create a new forum entry and we'll discuss it further there.

Martin.
Cyclos v4.8.2 w/ Social license
Technical Manager, Bristol Pound CIC, Bristol, UK, http://bristolpound.org

Post Reply