Art-of-WiFi / UniFi-API-browser

Tool to browse data exposed by Ubiquiti's UniFi Controller API
MIT License
1.1k stars 150 forks source link

UniFi controller login failure #89

Closed sMuve closed 3 years ago

sMuve commented 3 years ago

Hi there,

I have setup the API browser and can access the site and can see the controller name that I have configured in the config.php, but it is not able to fetch the sites from our controller.

  1. I have entered the credentials in the config.php and also in the Client.php
  2. I have tested the URL which I have used in both files and also the credentials. Works fine when I use them to directly log me in on the Unifi controller. The site itself shows me this error: We encountered the following error: UniFi controller login failure, please check your credentials in config/config.php!

As I wanted to get some more info on the error I have activated the debugging and checked the error.log. It shows me this error message: [Fri Jun 11 11:58:15.629448 2021] [php7:notice] [pid XXXXXX] [client XXX.XXX.XXX.XXX:XXXX] PHP Notice: HTTP response status received: 400. Probably a controller login failure in /var/www/html/UniFi-API-browser/vendor/art-of-wifi/unifi-api-client/src/Client.php on line 206, referer: http://XXX.XXX.XXX.XXX/UniFi-API-browser/

I'm not really familiar with PHP. What could I do to investigate this error further?

Thank in advance!

PS: Do I understand it correctly that I don't need to install the API client, because the API browser is including it?

malle-pietje commented 3 years ago

The 400 error usually is a sign that the credentials are incorrect or that the URL is incorrect. Without knowing the type of admin credentials you used, the controller version, and the platform it is running on (CloudKey, UDM, UDM PRO, software-install) there's not much I can say. Beside the mentioned points, also make sure to use the correct port (443/8443) and correct protocol (https instead of http).

malle-pietje commented 3 years ago

Regarding your question about the API client; yes, it comes included with the API Browser but for your own projects I highly recommend using composer to install the API client for each of your projects:: https://github.com/Art-of-WiFi/UniFi-API-client#composer

That makes it so much easier to pull updates etc..

sMuve commented 3 years ago

Thank you for your response. I have tried two ways to get it working, but first I will give you a little more insight on the infrastructure.

We are using a software-install of the UniFi controller (version 5.12.35) on one server. Then I have setup the API-client (and later on the API-browser) in a VM (Ubuntu 20.04 LTS with PHP 7.2 and cURL and the path for the installation is /var/www/html). The VM is completely connected to the server and reachable through port 8443. The user on our UniFi controller, which we want to use for the API client is an Administrator.

My first attempt was just to use the API-client (installed via Composer). I have setup the config.php and wanted to use the disable_switch_port.php to deactivate PoE for certain ports. Therefore you have documented in that I need to call the list_portconf() function of the Client.php. Unfortunately I'm not that familiar with PHP. I tried to call it via a php script which is located in the same folder as the Client.php and looks like this:

<?php

require_once(__DIR__.'/Client.php');

    $baseurl            = 'https://XXX.XXX.XXX.XXX:8443';
    $user               = 'XXX';
    $password           = 'XXX';
    $site               = 'default';
    $version            = '5.12.35';
    $ssl_verify         = false;

    $client = new UniFi_API\Client($user, $password, $baseurl, $site, $version, $ssl_verify);
    $portconf = $client->list_portconf();
    echo(var_export($portconf,1));
?>

NOTE:

The output is just "false".

So because I thought that I'm not able to call functions properly in PHP due to my lack of PHP knowledge, I found the API-browser and installed it in the VM, too. For the API-browser I have just edited the config-template.php in the config folder and saved it as config.php in the config folder. I was able to see the API browser and could also choose my controller, but when it was trying to fetch the sites I have received the error message mentioned in my first post. So I have tried the URL with https (and also with http) in my browser and logged in with the credentials of our API-user, which has worked without any issues. Then I tried to debug the error and had a look in the error log, which contains the 400 error mentioned above. So I was checking the credentials again and they were working. At this point I was more clueless then before and tried to investigate the Client.php as to check line 206. Then I saw that there are credentials, too. I have entered my credentials there, too (just to give it a try). As you can imagine that didn't work. So I have added the line error_log(var_export($ch,1)); Right after your comment

        /**
         * execute the cURL request and get the HTTP response code
         */`

The output is a bit long and contains sensitive information, but I think the most important line is at the very end:

> < Connection: close
> < 
> * Excess found: excess = 62 url = / (zero-length body)
> [Sat Jun 12 10:00:29.243554 2021] [php7:notice] [pid XXXXXX] [client XXX.XXX.XXX.XXX:XXX] NULL, referer: http://XXX.XXX.XXX.XXX/UniFi-API-browser/
> [Sat Jun 12 10:00:29.249393 2021] [php7:notice] [pid XXXXXX] [client XXX.XXX.XXX.XXX:XXX] PHP Notice:  HTTP response status received: 400. Probably a controller login failure in /var/www/html/UniFi-API-browser/vendor/art-of-wifi/unifi-api-client/src/Client.php on line 207, referer: http://XXX.XXX.XXX.XXX/UniFi-API-browser/

So at this point I'm not sure what else I could do. I would prefer to use the API-browser (just in case we want to use more functions where we need call a certain function of the API).

Let me know, if I can provide more infos to give you a better overview. Also thanks in advance for your time and assistance. I really appreciate it.

malle-pietje commented 3 years ago

I'm afraid I'm unable to provide PHP support here. Not meaning to be rude but you might be overreaching yourself with this...