picklepete / pyicloud

A Python + iCloud wrapper to access iPhone and Calendar data.
MIT License
2.5k stars 444 forks source link

Ubiquity key error dsid #333

Open zimo-github opened 3 years ago

zimo-github commented 3 years ago

The problem

print(api.files.dir()) throws a key error:

File "/Users/zimo/pyicloud/lib/python3.8/site-packages/pyicloud/services/ubiquity.py", line 43, in getattr return getattr(self.root, attr) File "/Users/zimo/pyicloud/lib/python3.8/site-packages/pyicloud/services/ubiquity.py", line 20, in root self._root = self.get_node(0) File "/Users/zimo/pyicloud/lib/python3.8/site-packages/pyicloud/services/ubiquity.py", line 29, in get_node request = self.session.get(self.get_node_url(node_id)) File "/Users/zimo/pyicloud/lib/python3.8/site-packages/pyicloud/services/ubiquity.py", line 25, in get_node_url return self._node_url % (self.params["dsid"], variant, node_id) KeyError: 'dsid'

Environment

Traceback/Error logs

Checklist

Additional information

Login and 2FA works fine, I can access iCloud Drive file info and device listing without errors. The error persists when I switch to python 3.9.4. Thanks a lot in advance for any help! Regards Thomas

Krammy commented 3 years ago

The same problem occuring on Windows 10 too, returning KeyError: 'dsid' when trying to access api.files.dir(). Python version: 3.7.2 32-bit.

Any ideas on how to fix this?

Example code: https://pastebin.com/raw/Nxc11Xx5

zimo-github commented 3 years ago

Sorry, didn’t have time to look into this in the meantime.

ZhangTianrong commented 3 years ago

It seems that pyicloud never attempts to retrieve the dsid or does it? What if I provide this in param manually? I will give it a try and report my findings here I guess.

Edit: It seems that if you do manually set the dsid, you can access ubiquity.

# based on https://stackoverflow.com/a/63868435
import requests
from bs4 import BeautifulSoup

principal_query = """\
<?xml version="1.0" encoding="utf-8"?>
<propfind xmlns='DAV:'>
    <prop>
        <current-user-principal/>
    </prop>
</propfind>
"""

def get_dsid(username, app_pass, caldav_url='https://caldav.icloud.com'):
    r = requests.request(
        'PROPFIND',
        url=caldav_url,
        data=principal_query,
        auth=requests.auth.HTTPBasicAuth(username, app_pass),
        headers={'Content-Type': 'application/xml'},
        )

    soup = BeautifulSoup(r.text, 'lxml')
    principal_extension = soup.find('current-user-principal').find('href').get_text()

    _ = [i for i in principal_extension.split(r"/") if i]
    assert len(_) == 2 and _[-1] == "principal", "Error: Cannot parse dsid"
    dsid = _[0]

    return dsid

Then you first initialize a api and set the dsid.

Krammy commented 3 years ago

Maybe this will be helpful for people, though I started using the SimpleNote app for note collection and reading instead. There's a Python API you can use for this.

moll commented 3 years ago

For those looking for a quick fix, get the DSID of yourself from the family member list (api.account.family) and pass it to api.files's UbiquityService:

api.files.params["dsid"] = api.account.family[0].dsid
zimo-github commented 3 years ago

Thanks a lot for the quick fix! Got a 503 Service unavailable today but I'm going to check that from time to time again.

shr00mie commented 3 years ago

to piggyback on @moll's post, if you don't have the family service:

api.files.params['dsid'] = api.data['dsInfo']['dsid']

now getting 503, just like @zimo-github

borismus commented 3 years ago

Seeing this exact thing, key error. Setting the dsid from family list gets rid of the error, but gives a 503 instead as described. Sadness

makbeaver commented 2 years ago

The same problem. Do you have any solutions? -Python V 3.9.5 -PyIcloud v 0.10.2

osheanlee commented 2 years ago

following this too. still encountering this problem

idiamant commented 2 years ago

I also encounter errors with this. When setting up dsid manually as: api.files.params['dsid'] = api.data['dsInfo']['dsid']

I now get Bad Request (400) when trying to use api.files.dir()

api.files.dir()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/idod/.virtualenvs/venv3/lib/python3.9/site-packages/pyicloud/services/ubiquity.py", line 97, in dir
    return [child.name for child in self.get_children()]
  File "/Users/idod/.virtualenvs/venv3/lib/python3.9/site-packages/pyicloud/services/ubiquity.py", line 92, in get_children
    self._children = self.connection.get_children(self.item_id)
  File "/Users/idod/.virtualenvs/venv3/lib/python3.9/site-packages/pyicloud/services/ubiquity.py", line 33, in get_children
    request = self.session.get(self.get_node_url(node_id, "parent"))
  File "/Users/idod/.virtualenvs/venv3/lib/python3.9/site-packages/requests/sessions.py", line 555, in get
    return self.request('GET', url, **kwargs)
  File "/Users/idod/.virtualenvs/venv3/lib/python3.9/site-packages/pyicloud/base.py", line 133, in request
    self._raise_error(response.status_code, response.reason)
  File "/Users/idod/.virtualenvs/venv3/lib/python3.9/site-packages/pyicloud/base.py", line 189, in _raise_error
    raise api_error
pyicloud.exceptions.PyiCloudAPIResponseException: Bad Request (400)
miku commented 2 years ago

If I'm printing out the URL of the request - from api.files.dir() - that yields a 503 I get something like:

https://p09-ubiquityws.icloud.com:443/ws/123123123/item/0

The Content-Length is 23 bytes and says:

Account migrated
despian commented 1 year ago

Also getting 503 after manually setting dsid.

Has anyone managed to resolve this?

a-hydrae commented 1 year ago

Found an answer: https://developer.apple.com/library/archive/technotes/tn2348/_index.html

MShirazAhmad commented 1 year ago

Did anyone find a solution to this?

Foxtrod89 commented 9 months ago

PyiCloudAPIResponseException: Service Unavailable (503)