Art-of-WiFi / UniFi-API-client

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

Login Does not Work with API #41

Closed Jack-Piotrowski-MAT-Holdings closed 5 years ago

Jack-Piotrowski-MAT-Holdings commented 5 years ago

Hi,

I have cloned the UniFi-API-client repository, loaded its libraries with composer and filled out the config appropriately with the correct username, password, https://*.*.*.*:8443, and my controller version.

I then created a new file 'login.php':


require_once("vendor\autoload.php"); require_once('config.php'); $unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); $set_debug_mode = $unifi_connection->set_debug($debug); $loginresults = $unifi_connection->login();


I do not get any errors on loading: require_once("vendor\autoload.php"); require_once('config.php');

Contents of config.php:


$controlleruser = 'admin'; // the user name for access to the UniFi Controller $controllerpassword = '*****'; // the password for access to the UniFi Controller $controllerurl = 'https://*.*.*.*:8443'; // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443' $controllerversion = '5.10.23'; // the version of the Controller software, eg. '4.6.6' (must be at least 4.0.0) $debug = false;


The login function simply returns "false", so it is not getting the $_SESSION['unificookie'] value.

I checked all of the settings on my controller's UI, and I did not find anything I needed to enable to allow calls like this.

Do you know of anything in the Controller UI that might fix this problem?

Maybe my client.php's config values?

client.php config values:


protected $baseurl            = 'https://127.0.0.1:8443';
protected $user               = 'admin';
protected $password           = '************';
protected $site               = 'https://*.*.*.*:8443';
protected $version            = '5.10.23';

Should the $baseurl be something other than my local host?

I appreciate any and all help!

Thank you,

Jack

malle-pietje commented 5 years ago

First of all, you should never need to change the contents of the Client.php file. The protected properties there have a purpose.

Secondly I believe you’re using an incorrect value for site_id, the fourth parameter for the constructor. Please read this section of the README: https://github.com/Art-of-WiFi/UniFi-API-client#important-notes

Jack-Piotrowski-MAT-Holdings commented 5 years ago

Okay, I just undid all changes I made to the Client.php file.

So now the constructor looks like this: image

I read the Important Notes section, went to my Controller's UI and found this in the url: https://:8443/manage/site/default/dashboard

So 'default' is my $site_id

So I changed the $site value (4th variable in Client.php's constructor) to 'default'

When I saved everything and refreshed the page that my login script is running it still returned 'false.'

Is there anything else that might be the culprit?

Thank you for your help,

Jack

malle-pietje commented 5 years ago

Can you enable debug mode (pass true without quotes) and see what the output is?

Jack-Piotrowski-MAT-Holdings commented 5 years ago

That debug response is actually for this code: (very slightly different than the code I posted before--taken from auth_guest_basic.php)

require_once("vendor\autoload.php"); require_once('config.php'); $mac = ''; $duration = 2000; $site_id = 'default'; $unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion); $set_debug_mode = $unifi_connection->set_debug($debug); $loginresults = $unifi_connection->login(); $auth_result = $unifi_connection->authorize_guest($mac, $duration); echo json_encode($auth_result, JSON_PRETTY_PRINT);

malle-pietje commented 5 years ago

Hmmm, I’m not seeing the string “default” in the URLs. Are you 100% sure you are passing the value in the correct way?

Jack-Piotrowski-MAT-Holdings commented 5 years ago

Well the 'default' string should be inserted into the URL inside of the login() function, right?

And I include the config and create a new instance of the Client class, so it should be getting the $site_id value from Client.php, which has it set to 'default'.

malle-pietje commented 5 years ago

Also make sure you pass a valid MAC address to this function.

Jack-Piotrowski-MAT-Holdings commented 5 years ago

I added a valid MAC address to the function. Still same results.

malle-pietje commented 5 years ago

Thats not how PHP OOP programming works. The constructor creates a new instance of the Class which has several specific properties such as the site id.

Jack-Piotrowski-MAT-Holdings commented 5 years ago

Oh, wait! I hard-coded 'default' as the site_id: $unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, 'default', $controllerversion);

And it returned true.

So what do I have to change to make it so $site_id in the below line of code actually refers to the $site value inside of Client.php?: $unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id, $controllerversion);

Something like this?

$unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $unifi_connection->site, $controllerversion);

Jack-Piotrowski-MAT-Holdings commented 5 years ago

I'm sorry, nevermind. I figured it out.

Thank you so very much for taking the time to help me.

I hope you have a wonderful morning/afternoon/night wherever you are!

Best regards,

Jack

malle-pietje commented 5 years ago

No problem, you're welcome. You too, here it's almost midnight (CET timezone).

By the way, feel free to share the resolution here for other readers.