Art-of-WiFi / UniFi-API-client

A PHP API client class to interact with Ubiquiti's UniFi Controller API
MIT License
1.09k stars 217 forks source link

HTTP response status received: 400. Probably a controller login failure #200

Closed tom26230 closed 4 months ago

tom26230 commented 8 months ago

Hello, I have a problem with the example files provided with the API client. I'm trying to test the API by executing the php file that lists the sites and I get this return in my console

PHP Notice:  The URL provided is incomplete, invalid or ends with a / character! in /var/www/guest/s/default/vendor/art-of-wifi/unifi-api-client/src/Client.php on line 3808
PHP Stack trace:
PHP   1. {main}() /var/www/guest/s/default/sites.php:0
PHP   2. UniFi_API\Client->__construct() /var/www/guest/s/default/sites.php:29
PHP   3. UniFi_API\Client->check_base_url() /var/www/guest/s/default/vendor/art-of-wifi/unifi-api-client/src/Client.php:75
PHP   4. trigger_error() /var/www/guest/s/default/vendor/art-of-wifi/unifi-api-client/src/Client.php:3808
PHP Notice:  HTTP response status received: 400. Probably a controller login failure in /var/www/guest/s/default/vendor/art-of-wifi/unifi-api-client/src/Client.php on line 206
PHP Stack trace:
PHP   1. {main}() /var/www/guest/s/default/sites.php:0
PHP   2. UniFi_API\Client->login() /var/www/guest/s/default/sites.php:31
PHP   3. trigger_error() /var/www/guest/s/default/vendor/art-of-wifi/unifi-api-client/src/Client.php:206
PHP Warning:  Invalid argument supplied for foreach() in /var/www/guest/s/default/sites.php on line 42
PHP Stack trace:
PHP   1. {main}() /var/www/guest/s/default/sites.php:0

What alarms me most is: PHP Notice: HTTP response status received: 400. Probably a controller login failure in /var/www/guest/s/default/vendor/art-of-wifi/unifi-api-client/src/Client.php on line 206

I've tested the API browser and it connects without a hitch. I created a new virtual controller but it's the same. Any idea how to debug this?

I'm sure it's a configuration problem on my side but I can't find it and I've been looking for a week when I get home from work and my wife is getting fed up!

Thanks

malle-pietje commented 8 months ago

Please share the full exact URL you’re using to connect to the controller. Feel free to only change the IP address or hostname.

tom26230 commented 8 months ago

https://10.100.10.50:8443

malle-pietje commented 8 months ago

Hmm, somehow an incorrect URL appears to be passed. Please double check your code and enable debug mode to see what is happening.

tom26230 commented 8 months ago

I also have another controller with the URL https://10.0.10.30:8443 but this is the same. I use the configuration file provided, without any modification apart from the controller information

const CLASS_VERSION = '1.1.80';
protected $baseurl   = 'https://10.100.10.50:8443';
protected $user  = 'api-login';
protected $password  = 'api-password';
protected $site   = 'default';
protected $version = '7.5.187';

I'm in a test environment and the api-login user is the local administrator of the Unifi controller, so there's no SSO or remote login.

In any case, I am infinitely grateful for your prompt reply.

malle-pietje commented 8 months ago

Hmm, please enable debug mode and share the full (but sanitized) output that you get when trying to login and fetch a collection, such as the sites.

malle-pietje commented 8 months ago

I also have another controller with the URL https://10.0.10.30:8443 but this is the same. I use the configuration file provided, without any modification apart from the controller information

const CLASS_VERSION = '1.1.80'; 
protected $baseurl = 'https://10.100.10.50:8443'; 
protected $user = 'api-login'; 
protected $password = 'api-password'; 
protected $site = 'default'; 
protected $version = '7.5.187';

I'm in a test environment and the api-login user is the local administrator of the Unifi controller, so there's no SSO or remote login.

In any case, I am infinitely grateful for your prompt reply.

Wait a minute... How are you configuring the URL and credentials to access the controller? From the above code, it looks as if you are editing the Client.php file.

You should not be doing that. Please use a separate config file as explained in the examples directory: https://github.com/Art-of-WiFi/UniFi-API-client/tree/master/examples