finish06 / pyunifi

https://unifi-sdn.ubnt.com/
MIT License
223 stars 99 forks source link

Issues with login into a Cloud Key Gen2 plus #62

Open Borgenstrand opened 3 years ago

Borgenstrand commented 3 years ago

I tried to use pyunifi against a Cloud Key Gen2 plus, and I found a login issue to it. I tried to use version="unifiOS" and then I got this error back: raise APIError("Login failed - status code: %i" % r.status_code) APIError: Login failed - status code: 401

I used F12 to login to the cloud key, and checked which URI it posted the login call to. It is supposted to be towards: https://IP/api/auth/login.

In the script, the self.url is: self.url = 'https://' + host + '/proxy/network/'

and the login function is using self.url like this: login_url = self.url + 'api/login'

Which means it gets /proxy/network/ before api/login.

So I changed the init from: if version == "unifiOS": self.host = host self.username = username self.password = password self.site_id = site_id self.ssl_verify = ssl_verify self.url = 'https://' + host + '/proxy/network/'

to: if version == "unifiOS": self.host = host self.username = username self.password = password self.site_id = site_id self.ssl_verify = ssl_verify self.url = 'https://' + host + '/proxy/network/' self.urllogin= 'https://' + host + '/' self.version=version

And the login function I changed from this: def _login(self): log.debug('login() as %s', self.username)

    # XXX Why doesn't passing in the dict work?
    params = {'username': self.username, 'password': self.password}
    login_url = self.url + 'api/login'

To: def _login(self): log.debug('login() as %s', self.username)

    # XXX Why doesn't passing in the dict work?
    params = {'username': self.username, 'password': self.password}
    if self.version=="v5":
        login_url = self.url + 'api/login'
    elif self.version=="unifiOS":
        login_url = self.urllogin + 'api/auth/login'

And then I could login to the Cloud key and pull everything I needed.

Just wanted to inform you about this issue, awesome job with the library!

holblin commented 3 years ago

@finish06 , I think this is fixed in the development branch and with UDMP-unifiOS version? When, did you plan to make a new release? This is also seems to block https://github.com/custom-components/sensor.unifigateway/issues/17 / https://github.com/custom-components/sensor.unifigateway/issues/35 and probably more.

geftactics commented 3 years ago

Here to express my excitement for a new release from dev branch! :)

finish06 commented 3 years ago

FYI: v2.21 has been uploaded to PyPi and is reported to have fixed these issues. Let me know what you find. Thanks.

mgottholsen commented 3 years ago

Tested it out today with a USG4 and a Cloud Key Gen 2, and unfortunately the 401 issue still exists. Is there a sample I can provide from my Unifi setup that will help?

finish06 commented 3 years ago

@mgottholsen - I do not have a CloudKey. Can you confirm the URL that is being accessed when logging into the CloudKey? Also, please provide the software version on the CloudKey2.

copart commented 2 years ago

Just wanted to say that I can use pyunifi (2.21 version) with my CK+ when I set version to UDMP-unifiOS.