[SOLVED] API Authentication - No Ticket

poxin

Renowned Member
Jun 27, 2017
73
7
73
Having an issue figuring out how to authenticate to PMG API via PHP.

I was able to generate a new ticket just fine, put passing in the cookie and header seems to be a problem for follow up requests. The API tells me:
HTTP/1.1 401 No ticket

PHP:
$pmgToken = Http::withOptions([
  'verify' => false,
])->post('https://IP:8006/api2/json/access/ticket', [
  'username' => 'root@pam',
  'password' => 'password',
]);

$pmgAuth = $pmgToken->json()['data'];

$response = Http::withOptions([
  'verify' => false,
  'debug' => true
])
  ->withHeaders([
    'CSRFPreventionToken' => $pmgAuth['CSRFPreventionToken'],
  ])
  ->withCookies(
    [
      'PMGAuthCookie' => $pmgAuth['ticket'],
    ],
    'https://IP:8006/api2/json/'
  )
  ->get('https://IP:8006/api2/json/version');

return $response;

I can verify the Cookie and Header are being set in the debug information

Code:
  +"cookies": GuzzleHttp\Cookie\CookieJar {#1071
    -cookies: array:1 [
      0 => GuzzleHttp\Cookie\SetCookie {#1072
        -data: array:9 [
          "Name" => "PMGAuthCookie"
          "Value" => "PMG:root@pam:60EF3D79::Co5NRGpF...(shortened)"
          "Domain" => "https://IP:8006/api2/json/"
          "Path" => "/"
 
The cookie domain should have just been the root domain/ip instead of the full path. Resolved.
 
  • Like
Reactions: fabian