[SOLVED] API Authentication - No Ticket

poxin

Well-Known Member
Jun 27, 2017
70
6
48
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

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!