mirror of
git://git.proxmox.com/git/pve-apiclient.git
synced 2025-03-12 04:58:26 +03:00
implement api token support
and add an example for it. Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
parent
f195667216
commit
7b6f8f1d17
@ -192,12 +192,13 @@ sub call {
|
|||||||
delete $self->{last_unknown_fingerprint};
|
delete $self->{last_unknown_fingerprint};
|
||||||
|
|
||||||
my $ticket = $self->{ticket};
|
my $ticket = $self->{ticket};
|
||||||
|
my $apitoken = $self->{apitoken};
|
||||||
|
|
||||||
my $ua = $self->{useragent};
|
my $ua = $self->{useragent};
|
||||||
|
|
||||||
# fixme: check ticket lifetime?
|
# fixme: check ticket lifetime?
|
||||||
|
|
||||||
if (!$ticket && $self->{username} && $self->{password}) {
|
if (!$ticket && !$apitoken && $self->{username} && $self->{password}) {
|
||||||
$self->login();
|
$self->login();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -346,9 +347,26 @@ sub new {
|
|||||||
|
|
||||||
$self->{useragent}->default_header('Accept-Encoding' => 'gzip'); # allow gzip
|
$self->{useragent}->default_header('Accept-Encoding' => 'gzip'); # allow gzip
|
||||||
|
|
||||||
$self->update_ticket($param{ticket}) if $param{ticket};
|
if ($param{apitoken} && $param{password}) {
|
||||||
|
warn "password will be ignored in favor of API token\n";
|
||||||
|
delete $self->{password};
|
||||||
|
}
|
||||||
|
if ($param{ticket}) {
|
||||||
|
if ($param{apitoken}) {
|
||||||
|
warn "ticket will be ignored in favor of API token\n";
|
||||||
|
} else {
|
||||||
|
$self->update_ticket($param{ticket});
|
||||||
|
}
|
||||||
|
}
|
||||||
$self->update_csrftoken($param{csrftoken}) if $param{csrftoken};
|
$self->update_csrftoken($param{csrftoken}) if $param{csrftoken};
|
||||||
|
|
||||||
|
if ($param{apitoken}) {
|
||||||
|
my $agent = $self->{useragent};
|
||||||
|
|
||||||
|
$self->{apitoken} = $param{apitoken};
|
||||||
|
|
||||||
|
$agent->default_header('Authorization', $param{apitoken});
|
||||||
|
}
|
||||||
|
|
||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
24
examples/example3.pl
Executable file
24
examples/example3.pl
Executable file
@ -0,0 +1,24 @@
|
|||||||
|
#!/usr/bin/perl
|
||||||
|
|
||||||
|
# NOTE: you need to set the API token and PVE host below for this to work
|
||||||
|
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
|
||||||
|
use PVE::APIClient::LWP;
|
||||||
|
|
||||||
|
use JSON;
|
||||||
|
|
||||||
|
my $apitoken = 'PVEAPIToken=USER@REALM!TOKENID=TOKENVALUE';
|
||||||
|
my $hostname = "127.0.0.1";
|
||||||
|
|
||||||
|
my $conn = PVE::APIClient::LWP->new(
|
||||||
|
apitoken => $apitoken,
|
||||||
|
host => $hostname,
|
||||||
|
# allow manual fingerprint verification
|
||||||
|
manual_verification => 1,
|
||||||
|
);
|
||||||
|
|
||||||
|
my $res = $conn->get("/access/permissions", {});
|
||||||
|
|
||||||
|
print to_json($res, { pretty => 1, canonical => 1});
|
Loading…
x
Reference in New Issue
Block a user