How to build a JWT RS256 Algorithm using Python?

137 Views Asked by At

How to fix the error "JWT method 'encode' does not exist" when trying to mount a base64 for the assertion parameter of an API?

I'm trying to create a base64 to use as an "assertion" parameter in an API call, but I'm getting an error saying that the "encode" method of the JWT does not exist.

I installed using:

pip3 install jwt

Full code

import jwt

private_key = open("C:\\file\\privateKey.pem", "r").read()

payload = {
    "iss": "iss",
    "aud": "aud",
    "scope": "*",
    "iat": 1683141898,
    "exp": 1683228298
}
signed = jwt.encode(payload, private_key, algorithm='RS256')

print(signed)

Error:

    signed = jwt.encode(payload, private_key, algorithm='RS256')
             ^^^^^^^^^^
AttributeError: module 'jwt' has no attribute 'encode'
1

There are 1 best solutions below

1
sahasrara62 On

you need to import JWT from jwt and then use encode as

from jwt import JWT
signed = JWT.encode(payload, private_key, algorithm='RS256')