how can I fixed api always return null

Rebirthz

Member
Jan 13, 2020
10
0
21
24
Hi,

I have use Proxmox API

When I'm using a root user it will return data

PHP:
$configure = [

    'hostname'  => 'proxmoxDomain',

    'username'  => 'root',

    'password'  => 'xxxxxxx',

];

Request::Login($configure);



$acl = [

    'userid' => "Test",

    'password' => 'Test',

    'email' => 'Test@Test.com',

    'enable' => '1',

];

$createUser  = Access::createUser($acl);

var_dump($createUser);

[data] => [errors] => stdClass Object ( [userid] => invalid format - value 'Test' does not look like a valid user name )

But when I'm using api user with Token

PHP:
$configure = [

    'hostname'  => 'proxmoxDomain',

    'username' => 'apiAccount',

    'token_name' => 'apiUsername',

    'token_value' => 'xxxxxxxx-xxxxxxxx-xxxxxxxxx',

];

Request::Login($configure);



$acl = [

    'userid' => "Test",

    'password' => 'Test',

    'email' => 'Test@Test.com',

    'enable' => '1',

];

$createUser  = Access::createUser($acl);

var_dump($createUser);

["data"]=> NULL

I don't know it about permission or something but I can get User in node normaly.


thanks!
 
Last edited:
Hi,

FYI, you need to add the realm here, e.g., for the built-in PVE realm: test@pve

A login with tokens should not be required.

What API binding/library do you use? As it either needs some other calls or might do something wrong in general when using tokens?

Hi, Thanks for helping!!!

I know about input @pve I'm try to test display error.


I'm using this https://github.com/Saleh7/ProxmoxVE_PHP_API

This is Login code
PHP:
public static function Login(array $configure, $verifySSL = false, $verifyHost = false)
  {
    $check = false;
    if (empty($configure['password'])) {
      if (empty($configure['token_name']) || empty($configure['token_value'])) {
        $check = true;
      } else {
        self::$token_name = $configure['token_name'];
        self::$token_value = $configure['token_value'];
      }
    } else {
      self::$password = $configure['password'];
    }
    self::$hostname     = !empty($configure['hostname'])      ? $configure['hostname']    :  '';
    self::$username     = !empty($configure['username'])      ? $configure['username']    : $check = true;
    self::$realm        = !empty($configure['realm'])         ? $configure['realm']       : 'pam'; // pam - pve - ..
    self::$port         = !empty($configure['port'])          ? $configure['port']        : 8006;
    if ($check) {
      throw new ProxmoxException(
        'Require in array [hostname], [username], [password] or [token_name] and [token_value], [realm], [port]'
      );
    }
    self::ticket($verifySSL, $verifyHost);
  }
    /**
     * Create or verify authentication ticket.
     * POST /api2/json/access/ticket
    */
  protected static function ticket($verifySSL, $verifyHost)
  {
    self::$Client = new \Curl\Curl();
    self::$Client->setOpts([
      CURLOPT_SSL_VERIFYPEER => $verifySSL,
      CURLOPT_SSL_VERIFYHOST => $verifyHost
    ]);
    if (self::$token_name && self::$token_value) {
      self::$Client->setHeader('Authorization', sprintf(
        'PVEAPIToken=%s!%s=%s',
        self::$username . '@' . self::$realm,
        self::$token_name,
        self::$token_value
      ));
    } else {
      $data = [
        'username'  => self::$username,
        'password'  => self::$password,
        'realm'     => self::$realm,
      ];
      $response = self::$Client->post("https://" . self::$hostname . ":" . self::$port . "/api2/json/access/ticket", $data);
      if (!$response) {
        throw new ProxmoxException('Request params empty');
      }
      // set header
      self::$Client->setHeader('CSRFPreventionToken', $response->data->CSRFPreventionToken);
      // set cookie
      self::$Client->setCookie('PVEAuthCookie', $response->data->ticket);
    }
    return true;
  }


For now, I'm testing with not use token
PHP:
$configure = [
    'hostname'  => 'proxmoxDomain',
    'username' => 'apiAccount',
    'password' => 'xxxxxxxxxxx',
];

$data = Request::Login($configure); // Login ..


$acl = [

    'userid' => "Test",

    'password' => 'Test',

    'email' => 'Test@Test.com',

    'enable' => '1',

];

$createUser  = Access::createUser($acl);

var_dump($createUser);

["data"]=> NULL

It return null same as before
 
Last edited:

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!