KACE -> Utilizing /api/assets/asset Endpoint
I am trying to connect to the KACE API to pull asset data. I am able to successfully authenticate but when I try to make a query to /api/assets/asset I am getting a 401 error with this body:
{"errorCode":-1,"errorDescription":"No Current Organization Selected"}
I have tried changing the dell-api-version (0 through 4 report "Not logged in", 5 through 10 state No Current Organization Selected).
I have tried sending the organizationName in the second query.
My code is:
$client = new GuzzleHttp\Client([ 'verify' => false ]);
$jar = new \GuzzleHttp\Cookie\CookieJar;
$res = $client->request('GET',"https://url/ams/shared/api/security/login", [ 'json' => ['userName' => 'redacted', 'password' => 'redacted', 'organizationName' => 'Default', ], 'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json', 'x-dell-api-version' => '1'], 'cookies' => $jar ]);
$kace = "";
foreach($jar->toArray() as $data){
if(preg_match('/KACE_CSRF_TOKEN/i', $data['Name'])) $kace = $data['Value'];
}
$qurl = 'https://url/api/asset/assets';
$res = $client->request('GET',$qurl, [ 'headers' => [ 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'x-dell-api-version' => '5', 'x-dell-csrf-token' => $kace ], 'cookies' => $jar ]);
$body = $res->getBody();
echo $body;?>
Answers (1)
Change your code to look for the header 'X-DELL-CSRF-TOKEN'
See working example below, also you can replace the" foreach" code with the following.
$kace = $res->getHeaderLine('X-DELL-CSRF-TOKEN');
<?php
require './vendor/autoload.php';
$client = new GuzzleHttp\Client();
$jar = new \GuzzleHttp\Cookie\CookieJar;
$res = $client->request('POST','http://192.168.1.245/ams/shared/api/security/login', [
'json' => ['userName' => 'admin',
'password' => 'password',
'organizationName' => 'Default'],
'headers' => ['Content-Type' => 'application/json',
'Accept' => 'application/json',
'x-dell-api-version' => '5'],
'cookies' => $jar
]);
$token = $res->getHeaderLine('X-DELL-CSRF-TOKEN');
$res = $client->request('GET','http://192.168.1.245/api/asset/assets/', [
'headers' => [ 'Content-Type' => 'application/json',
'Accept' => 'application/json',
'x-dell-api-version' => '5',
'X-DELL-CSRF-TOKEN' => $token ],
'cookies' => $jar
]);
$body = $res->getBody();
echo $body;
When I print the $res->getHeaderLine('X-DELL-CSRF-TOKEN') there is no value. This is why I switched to looking through each of the cookies for the KACE CSRF and pulling that value as the token.
When I try to POST to the security login I get a 500 error (no route found). - dale-muted 3 years ago
When I output the $res->getHeaderLine('X-DELL-CSRF-TOKEN') it is blank. - dale-muted 3 years ago