rschroll / rmcl

reMarkable Cloud Library for Python
MIT License
28 stars 7 forks source link

Error when trying to access cloud API #1

Closed cycomanic closed 3 years ago

cycomanic commented 3 years ago

I see an error when trying to access the cloud API. It seems like the access token does not work properly. See below

[ins] In [6]: rmcl.register_device_s("evdfgryb")
Out[6]: True

[ins] In [7]: rmcl.Item.get_by_id_s('')
Failed to decode JSON from bytearray(b'Serving request failed, Msg: invalid token: token contains an invalid number of segments: invalid token, Origin: invalid token: token contains an invalid number of segments: invalid token, HTTPCode: 401\n')
Response code: 401
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
~/.local/lib/python3.8/site-packages/rmcl/api.py in update_items(self)
    213         try:
--> 214             response_json = response.json()
    215         except json.decoder.JSONDecodeError:

~/.local/lib/python3.8/site-packages/asks/response_objects.py in json(self, **kwargs)
    103         body = self._decompress(self.encoding)
--> 104         return _json.loads(body, **kwargs)
    105 

/usr/lib64/python3.8/json/__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    356             parse_constant is None and object_pairs_hook is None and not kw):
--> 357         return _default_decoder.decode(s)
    358     if cls is None:

/usr/lib64/python3.8/json/decoder.py in decode(self, s, _w)
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()

/usr/lib64/python3.8/json/decoder.py in raw_decode(self, s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

ApiError                                  Traceback (most recent call last)
<ipython-input-7-31c6a7717613> in <module>
----> 1 rmcl.Item.get_by_id_s('')

~/.local/lib/python3.8/site-packages/rmcl/items.py in get_by_id_s(id_)
     49     @staticmethod
     50     def get_by_id_s(id_):
---> 51         return trio.run(Item.get_by_id, id_)
     52 
     53     @classmethod

    [... skipping hidden 1 frame]

~/.local/lib/python3.8/site-packages/rmcl/items.py in get_by_id(id_)
     43     @staticmethod
     44     async def get_by_id(id_):
---> 45         return await (await api.get_client()).get_by_id(id_)
     46 
     47     # Decorating a staticmethod is not trivial.  Since this is the only one,

~/.local/lib/python3.8/site-packages/rmcl/api.py in get_by_id(self, id_)
    243         async with self.update_lock:
    244             if not self.refresh_deadline or now() > self.refresh_deadline:
--> 245                 await self.update_items()
    246 
    247         return self.by_id[id_]

~/.local/lib/python3.8/site-packages/rmcl/api.py in update_items(self)
    216             log.error(f"Failed to decode JSON from {response.content}")
    217             log.error(f"Response code: {response.status_code}")
--> 218             raise ApiError("Failed to decode JSON data")
    219 
    220         old_ids = set(self.by_id) - {'', 'trash'}

ApiError: Failed to decode JSON data
lckarssen commented 3 years ago

I received a similar response when trying to connect to the rM cloud with rmfuse:

Failed to decode JSON from bytearray(b'Serving request failed, Msg: invalid token: token contains an invalid number of segments: invalid token, Origin: invalid token: token contains an invalid number of segments: invalid token, HTTPCode: 401\n')
Response code: 401
Traceback (most recent call last):
  File "/home/lennart/.local/lib/python3.8/site-packages/rmcl/api.py", line 198, in update_items
    response_json = response.json()
  File "/home/lennart/.local/lib/python3.8/site-packages/asks/response_objects.py", line 104, in json
    return _json.loads(body, **kwargs)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lennart/.local/bin/rmfuse", line 8, in <module>
    sys.exit(main())
  File "/home/lennart/.local/lib/python3.8/site-packages/rmfuse/fuse.py", line 396, in main
    trio.run(pyfuse3.main)
  File "/home/lennart/.local/lib/python3.8/site-packages/trio/_core/_run.py", line 1932, in run
    raise runner.main_task_outcome.error
  File "/home/lennart/.local/lib/python3.8/site-packages/_pyfuse3.py", line 30, in wrapper
    await fn(*args, **kwargs)
  File "src/pyfuse3.pyx", line 773, in main
  File "/home/lennart/.local/lib/python3.8/site-packages/trio/_core/_run.py", line 815, in __aexit__
    raise combined_error_from_nursery
  File "/home/lennart/.local/lib/python3.8/site-packages/_pyfuse3.py", line 30, in wrapper
    await fn(*args, **kwargs)
  File "src/internal.pxi", line 272, in _session_loop
  File "src/handlers.pxi", line 104, in fuse_getattr_async
  File "/home/lennart/.local/lib/python3.8/site-packages/rmfuse/fuse.py", line 193, in getattr
    item = await self.get_by_id(self.get_id(inode))
  File "/home/lennart/.local/lib/python3.8/site-packages/rmfuse/fuse.py", line 138, in get_by_id
    return await Item.get_by_id(id_)
  File "/home/lennart/.local/lib/python3.8/site-packages/rmcl/items.py", line 46, in get_by_id
    return await (await api.get_client()).get_by_id(id_)
  File "/home/lennart/.local/lib/python3.8/site-packages/rmcl/api.py", line 229, in get_by_id
    await self.update_items()
  File "/home/lennart/.local/lib/python3.8/site-packages/rmcl/api.py", line 202, in update_items
    raise ApiError("Failed to decode JSON data")
rmcl.exceptions.ApiError: Failed to decode JSON data
sergei-mironov commented 3 years ago

Same here, rmfuse version 0.2.1, rmcl version 0.4.0

[nix-shell:~/proj/remarkable]$ rmfuse -v -m orig  _remarkable/
DEBUG:llfuse:Initializing llfuse
DEBUG:llfuse:Calling fuse_mount
DEBUG:llfuse:Calling fuse_lowlevel_new
DEBUG:llfuse:Calling fuse_session_add_chan
DEBUG:rmfuse.fuse:Mounting on _remarkable/
DEBUG:llfuse:Calling fuse_session_loop
DEBUG:rmcl.api:Got 401 code; trying to renew token
DEBUG:rmcl.api:Renewing user token
ERROR:rmcl.api:Failed to decode JSON from bytearray(b'Serving request failed, Msg: invalid token: token contains an invalid number of segments: invalid token, Origin: invalid token: token contains an invalid number of segments: invalid token, HTTPCode: 401\n')
ERROR:rmcl.api:Response code: 401
INFO:llfuse:handler raised <class 'rmcl.exceptions.ApiError'> exception (Failed to decode JSON data), terminating main loop.
DEBUG:llfuse:Terminated main loop because request handler raised exception, re-raising..
DEBUG:llfuse:Calling fuse_session_remove_chan
DEBUG:llfuse:Calling fuse_session_destroy
DEBUG:llfuse:Calling fuse_unmount
cycomanic commented 3 years ago

@grwlf @lckarssen see my pull request #2 on how to fix this.

lckarssen commented 3 years ago

I confirm that the patch in PR #2 works. :+1:

Thanks for the quick solution @cycomanic!

sergei-mironov commented 3 years ago

2 works for me too