python-caldav / caldav

Apache License 2.0
313 stars 91 forks source link

PropfindError when try to access client.principal() #375

Closed seanmills1020 closed 5 months ago

seanmills1020 commented 5 months ago

Hello,

If I use my account to authenticate, everything is fine. However, if I try to authenticate using the credentials from a system account, I get a PropfindError error. Here is the traceback.

Maybe the system account doesn’t exist?

Traceback (most recent call last):
  File "/Users/seanmills/Desktop/Python 100 Days of Code/Projects/CalDAV/main.py", line 151, in <module>
    principal = dav_client.principal()
                ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/seanmills/Desktop/Python 100 Days of Code/Projects/CalDAV/venv/lib/python3.11/site-packages/caldav/davclient.py", line 425, in principal
    self._principal = Principal(client=self, *largs, **kwargs)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/seanmills/Desktop/Python 100 Days of Code/Projects/CalDAV/venv/lib/python3.11/site-packages/caldav/objects.py", line 506, in __init__
    cup = self.get_property(dav.CurrentUserPrincipal())
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/seanmills/Desktop/Python 100 Days of Code/Projects/CalDAV/venv/lib/python3.11/site-packages/caldav/objects.py", line 224, in get_property
    foo = self.get_properties([prop], **passthrough)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/seanmills/Desktop/Python 100 Days of Code/Projects/CalDAV/venv/lib/python3.11/site-packages/caldav/objects.py", line 249, in get_properties
    response = self._query_properties(props, depth)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/seanmills/Desktop/Python 100 Days of Code/Projects/CalDAV/venv/lib/python3.11/site-packages/caldav/objects.py", line 172, in _query_properties
    return self._query(root, depth)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/seanmills/Desktop/Python 100 Days of Code/Projects/CalDAV/venv/lib/python3.11/site-packages/caldav/objects.py", line 216, in _query
    raise error.exception_by_method[query_method](errmsg(ret))
caldav.lib.error.PropfindError: PropfindError at '503 Service Unavailable

b'Service Unavailable'', reason no reason
tobixen commented 5 months ago

What is "system account" and what is "user account" in this context, and what kind of server do you try to connect to?

seanmills1020 commented 5 months ago

Sorry for not responding sooner. The issue was that the account in question had not been created yet.

I have a follow-up question. When you try to authenticate using caldav.DAVClient(), do you typically wrap this call in a try-except block?

tobixen commented 5 months ago

The construct client = caldav.DAVClient() doesn't initiate any communication by itself, only when you do things like client.principal() it will try to log in.

The "modern" way to use the DAVClient is to wrap it into a with-block:

with caldav.DAVClient() as client:
    principal = client.principal()
    ...

It shouldn't matter much, but the with-construct ensures some cleanup is done when the DAVClient-object is no longer in use.

If you want to catch problems like wrong password, then use a try/except block around the principal call (meta-code, not tested at all and is likely to contain errors):

with caldav.DAVClient() as client:
    try:
        principal = client.principal()
    except caldav.lib.error.AuthorizationError:
        print("probably the username/password is incorrect")
        raise
    try:
        calendar = principal.calendar()
        calendar.save_event(...)
    except caldav.lib.error.DAVError:
        print("something went wrong while trying to add the event to the calendar")
        raise