Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

ValueError: Unable to load PEM file.

Open XariZaru opened this issue 1 year ago • 1 comments

I received the following issue:

Some algorithms requires "pip install cryptography". See https://pyjwt.readthedocs.io/en/latest/installation.html#cryptographic-dependencies-optional
Traceback (most recent call last):
  File "C:\Users\jnguyen-a\AppData\Local\Programs\Python\Python311\Lib\site-packages\jwt\algorithms.py", line 350, in prepare_key
    RSAPrivateKey, load_pem_private_key(key_bytes, password=None)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jnguyen-a\AppData\Local\Programs\Python\Python311\Lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 494, in _handle_key_loading_error
    raise ValueError(
ValueError: ('Could not deserialize key data. The data may be in an incorrect format, it may be encrypted with an unsupported algorithm, or it may be an unsupported key type (e.g. EC curves with explicit parameters).', [<OpenSSLError(code=503841036, lib=60, reason=524556, reason_text=unsupported)>])

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\jnguyen-a\AppData\Local\Programs\Python\Python311\Lib\site-packages\msal\oauth2cli\assertion.py", line 114, in create_normal_assertion
    str_or_bytes = jwt.encode(  # PyJWT 1 returns bytes, PyJWT 2 returns str
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jnguyen-a\AppData\Local\Programs\Python\Python311\Lib\site-packages\jwt\api_jwt.py", line 73, in encode
    return api_jws.encode(
           ^^^^^^^^^^^^^^^
  File "C:\Users\jnguyen-a\AppData\Local\Programs\Python\Python311\Lib\site-packages\jwt\api_jws.py", line 160, in encode
    key = alg_obj.prepare_key(key)
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jnguyen-a\AppData\Local\Programs\Python\Python311\Lib\site-packages\jwt\algorithms.py", line 353, in prepare_key
    return cast(RSAPublicKey, load_pem_public_key(key_bytes))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Unable to load PEM file. See https://cryptography.io/en/latest/faq/#why-can-t-i-import-my-pem-file for more details. MalformedFraming```

My .PEM has a format similar to below:

```python
-----BEGIN CERTIFICATE-----
fawefaojsfdojiwefoajsdkljskdfasf=wejfasidofjlksdjfalskdjfkwef
asdkfjeiorjqipofkdkdkdfowifeasdfdkfas
sadfoijweoifjaskldfjlskd
-----END CERTIFICATE-----

This is my ClientContext code

cert_credentials = {
    "tenant": TENANT,
    "client_id": CLIENT_ID,
    "thumbprint": THUMBPRINT,
    "private_key": cert_pem_content
}

print(os.getcwd())

ctx = ClientContext("https://COMPANY.sharepoint.com/").with_client_certificate(**cert_credentials)
current_web = ctx.web.get().execute_query()
print("{0}".format(current_web.url))

XariZaru avatar May 08 '24 19:05 XariZaru

Hi, I had the same problem but it was solved with issue #700.

dulalbert avatar Jun 13 '24 08:06 dulalbert