JWT with Python SDK giving "Could not unserialize key data."

New Contributor

JWT with Python SDK giving "Could not unserialize key data."

I am using Python 3.5.  Here is my code,which is straight out of the docs, followed by the error.   
 
from boxsdk import JWTAuth
 
auth = JWTAuth(
    client_id='k29xaarbpae4znkghpay6rsixiitg1xc',
    client_secret='AajZ60HOdKhsuX0PRe0mkv5agV1J61sf',
    enterprise_id='XXXXXXX',
    jwt_key_id='XXXXX',
    rsa_private_key_file_sys_path='C:\\Users\\H470722\\Documents\\Keys\\private_key.pem',
    rsa_private_key_passphrase='XXXXXXX',
    store_tokens=lambda x, y: None, 
)
 
access_token = auth.authenticate_instance()
 
 
 
Error:
 
>>> 
 RESTART: C:/Users/H470722/Documents/EIV Reporting ETL/Python Scripts/BoxTest.py 
From cffi callback <function _pem_password_cb at 0x042AD390>:
Traceback (most recent call last):
  File "C:\Users\H470722\AppData\Roaming\Python\Python35\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 101, in _pem_password_cb
    pw_buf[:len(ud.password)] = ud.password
TypeError: a bytes-like object is required, not 'str'
Traceback (most recent call last):
  File "C:/Users/H470722/Documents/EIV Reporting ETL/Python Scripts/BoxTest.py", line 10, in <module>
    store_tokens=lambda x, y: None,
  File "C:\Users\H470722\AppData\Roaming\Python\Python35\site-packages\boxsdk\auth\jwt_auth.py", line 102, in __init__
    backend=default_backend(),
  File "C:\Users\H470722\AppData\Roaming\Python\Python35\site-packages\cryptography\hazmat\primitives\serialization.py", line 20, in load_pem_private_key
    return backend.load_pem_private_key(data, password)
  File "C:\Users\H470722\AppData\Roaming\Python\Python35\site-packages\cryptography\hazmat\backends\multibackend.py", line 305, in load_pem_private_key
    return b.load_pem_private_key(data, password)
  File "C:\Users\H470722\AppData\Roaming\Python\Python35\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 1084, in load_pem_private_key
    password,
  File "C:\Users\H470722\AppData\Roaming\Python\Python35\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 1253, in _load_key
    self._handle_key_loading_error()
  File "C:\Users\H470722\AppData\Roaming\Python\Python35\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 1325, in _handle_key_loading_error
    raise ValueError("Could not unserialize key data.")
ValueError: Could not unserialize key data.
 
 
What am I doing wrong?
9 REPLIES
New Contributor

Re: JWT with Python SDK giving "Could not unserialize key data."

I'm also running into this same issue.

Box Employee

Re: JWT with Python SDK giving "Could not unserialize key data."

@Patricia65 @uberscout I am not sure why you are seeing this error, but according to this thread it might be an issue with the format of the private key you are using.

New Contributor

Re: JWT with Python SDK giving "Could not unserialize key data."

I was looking at the Box SDK and it looks like it's in the JWTAuth object (jwt_auth.py) specifically:

 

```

with open(rsa_private_key_file_sys_path) as key_file:
self._rsa_private_key = serialization.load_pem_private_key(
key_file.read(),
password=rsa_private_key_passphrase,
backend=default_backend(),
)

```

key_file.read() is passing it a string and it expects a bytes array (https://cryptography.io/en/latest/hazmat/primitives/asymmetric/serialization/#cryptography.hazmat.pr...)

 

I am just manually writing my JWT Auth but I believe the key_file.read() should be wrapped with bytes.

 

```

with open(rsa_private_key_file_sys_path) as key_file:
self._rsa_private_key = serialization.load_pem_private_key(
bytes(key_file.read()),
password=rsa_private_key_passphrase,
backend=default_backend(),
)

```

New Contributor

Re: JWT with Python SDK giving "Could not unserialize key data."

Looks like this was fixed in an update. You should be able to do this now.

Occasional Visitor

Re: JWT with Python SDK giving "Could not unserialize key data."

How do I fix this in Python 2.7?

Regular Visitor

Re: JWT with Python SDK giving "Could not unserialize key data."

Which version was it fixed in? I am using 3.6 and still facing the issue.

Tags (1)
Occasional Contributor

Re: JWT with Python SDK giving "Could not unserialize key data."

Same here...

Anyone have had success?

Occasional Contributor

Re: JWT with Python SDK giving "Could not unserialize key data."

I was not decrypting my private key -- duh!

Highlighted
Visitor

Re: JWT with Python SDK giving "Could not unserialize key data."

I don't know if this will help now or not but, you need to ensure that rsa_private_key_passphrase is in bytes and not string.

So instead of -

rsa_private_key_passphrase='XXXXXXX',

use -

rsa_private_key_passphrase=b'XXXXXXX', (for bytes)

Users online (1,176)