Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Failed to parse JSON object as TDLib request: Wrong padding length" When tried to login #81

Closed
VenkatTeja opened this issue Mar 3, 2020 · 8 comments
Assignees

Comments

@VenkatTeja
Copy link

File "/app/index.py", line 17, in
tg.login()
File "/usr/local/lib/python3.8/site-packages/telegram/client.py", line 439, in login
result.wait(raise_exc=True)
File "/usr/local/lib/python3.8/site-packages/telegram/utils.py", line 41, in wait
raise RuntimeError(f'Telegram error: {self.error_info}')
RuntimeError: Telegram error: {'@type': 'error', 'code': 400, 'message': 'Failed to parse JSON object as TDLib request: Wrong padding length', '@extra': {'request_id': 'updateAuthorizationState'}}

Code is running in docker!

Thank you

@alexander-akhmetov
Copy link
Owner

Hi,

On which authorisation state does it fail? It should print it to the logs.

@CatCoderr
Copy link

I have the same problem, here is logs: https://pastebin.com/qAUQr5Ck

@alexander-akhmetov alexander-akhmetov self-assigned this Mar 13, 2020
@alexander-akhmetov
Copy link
Owner

alexander-akhmetov commented Mar 13, 2020

I see, thanks for the logs. Probably it's related to the encryption key (parameter database_encryption_key).

While it's not fixed in python-telegram, can you try to make the database encryption key string longer? Probably you'll need to remove existing tdlib database. By default python-telegram keeps it in /tmp/.tdlib_files/

tg = Telegram(
    database_encryption_key='...',
)

@steven-chau
Copy link

I have the exact same problem! Same error. Same stacktrace.

I have tried a long database_encryption_key and a short one. No luck :(

Perhaps the protocol has changed? The underlying tdLib library needs to be upgraded?

@jonasborges
Copy link

I see, thanks for the logs. Probably it's related to the encryption key (parameter database_encryption_key).

While it's not fixed in python-telegram, can you try to make the database encryption key string longer? Probably you'll need to remove existing tdlib database. By default python-telegram keeps it in /tmp/.tdlib_files/

tg = Telegram(
    database_encryption_key='...',
)

It seems to be the parameter name being sent.

The example provided by tdlib uses key as parameter for checkDatabaseEncryptionKey messages.

{'@type': 'checkDatabaseEncryptionKey', 'key': '...'}

While python-telegram is using encryption_key.
The documentation also mention encryption_key.

So, I made it work by changing the key name from encryption_key to key. I'm not sure if this is the proper solution but works for now.

@alexander-akhmetov
Copy link
Owner

alexander-akhmetov commented Mar 24, 2020

The example provided by tdlib uses key as parameter for checkDatabaseEncryptionKey messages.

Hm, interesting! I'm not sure if it's a mistake or they are interchangeable. I will try to look into that.

I also found that in td_api.tl encryption_key has type bytes and in the documentation it is string. They mentioned in one of the issues that bytes should be sent as base64 encoded string. The error original error message (Wrong padding length) is also related to base64.

With base64 encoded string it seems to be working, but I don't know yet how to check if tdlib is using the correct and decoded from base64 key, or just the passed string:

    def _send_encryption_key(self) -> AsyncResult:
        logger.info('Sending encryption key')

        key = self._database_encryption_key
        if isinstance(key, str):
            key = key.encode()

        data = {
            '@type': 'checkDatabaseEncryptionKey',
            'encryption_key': base64.b64encode(key).decode(),
        }

        return self._send_data(data, result_id='updateAuthorizationState')

I asked a question in tdlib repository about base64 and difference between key and encryption_key: tdlib/td#970


Can somebody try to use the default key from the python-telegram examples (database_encryption_key='changeme1234') and an empty key (database_encryption_key=''), please?

@alexander-akhmetov
Copy link
Owner

alexander-akhmetov commented Mar 24, 2020

I asked a question in tdlib repository about base64 and difference between key and encryption_key: tdlib/td#970

Yes, it has bytes type and should be encoded using base64, I'll make changes to python-telegram.

While it's not fixed, you can try to do it on your side: use a different key with a different length or encode encryption key using base64 and pass it to Telegram:

import base64

...

encryption_key = base64.b64encode(b'secret').decode()

tg = Telegram(
    ...
    database_encryption_key=encryption_key,
)

alexander-akhmetov added a commit that referenced this issue Mar 29, 2020
…tion-key

Fix #81 Encode database encrypting key using base64
@alexander-akhmetov
Copy link
Owner

alexander-akhmetov commented Mar 29, 2020

Released 0.12.0 (both the package and the docker image), if you still see the problem, please re-open the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants