# Why am I getting this error when trying to auth?

**URL:** <https://developer.community.nhs.uk/t/why-am-i-getting-this-error-when-trying-to-auth/668>\
**Category:** General enquiries\
**Tags:** testing, authentication\
**Created:** [12 January 2024 13:19 UTC](https://developer.community.nhs.uk/t/why-am-i-getting-this-error-when-trying-to-auth/668 "2024-01-12T13:19:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil.glew](https://avatars.discourse-cdn.com/v4/letter/p/e68b1a/32.png) [@phil.glew](https://developer.community.nhs.uk/u/phil.glew)\
**Post date:** [12 January 2024 13:19 UTC](https://developer.community.nhs.uk/t/why-am-i-getting-this-error-when-trying-to-auth/668/1 "2024-01-12T13:19:01Z")

</div>

Hi,

I have an NHS Developer account, and have Enabled access to the Hello World and Personal Demographics Service APIs (both application-restricted and healthcare worker). I have uploaded a valid JWKS JSON and a callback URL.

I have also created a signed JWT, using the API Key value from my application in, using my .PEM which I created via BASH.

However, when I try now to use the signed JWT in a POST API to get an access token to [https://dev.api.service.nhs.uk/oauth2/token](https://dev.api.service.nhs.uk/oauth2/token)

I have been following [this](https://digital.nhs.uk/developer/guides-and-documentation/security-and-authorisation/application-restricted-restful-apis-signed-jwt-authentication#step-3-register-your-public-key-with-us)

I am receiving ALWAYS the below in Postman. Am I doing something wrong or missing something claring?

 ![image](https://europe1.discourse-cdn.com/nhsdigitaldeveloper/original/1X/0c32bca3d75cb90987c03a63258bc82aceb93498.png)

Thanks  
Phil

Below are my scripts in Python:

```auto
import datetime
import jwt
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
import uuid

# Your header and payload
header = {
    "alg": "RS512",
    "typ": "JWT",
    "kid": "test1"
}

payload = {
    "iss": "REDACTED",
    "sub": "REDACTED",
    "aud": "https://dev.api.service.nhs.uk/oauth2/token",
    "jti": str(uuid.uuid4()),
    "exp": datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(minutes=5)
}

# Load your RSA private key
with open("C:\\Users\\PhilGlew\\test1.pem", "rb") as key_file:
    private_key = serialization.load_pem_private_key(
        key_file.read(),
        password=None,
        backend=default_backend()
    )

# Generate the JWT token
encoded_jwt = jwt.encode(payload, private_key, algorithm="RS512", headers=header)
print(encoded_jwt)

try:
    encoded_jwt = jwt.encode(payload, private_key, algorithm="RS512", headers=header)
    print("JWT Token:", encoded_jwt) # To verify the token is generated

    # Save the JWT token to a file
    with open("C:\\Users\\PhilGlew\\jwtToken.txt", "w") as token_file:
        token_file.write(encoded_jwt) # No need to decode
        print("Token written to jwtToken.txt") # Confirmation message

except Exception as e:
    print("An error occurred:", e)

```

and

```auto
import uuid
from time import time
import jwt # https://github.com/jpadilla/pyjwt

with open("C:\\Users\\PhilGlew\\test1.pem", "r") as f:
  private_key = f.read()

claims = {
  "sub": "REDACTED",
  "iss": "REDACTED",
  "jti": str(uuid.uuid4()),
  "aud": "https://dev.api.service.nhs.uk/oauth2/token",
  "exp": int(time()) + 300, # 5mins in the future
}

additional_headers = {"kid": "test1"}

j = jwt.encode(
  claims, private_key, algorithm="RS512", headers=additional_headers
)

```

---

<div class="post-metadata">

**Author:** ![hameed](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@hameed](https://developer.community.nhs.uk/u/hameed)\
**Post date:** [14 January 2024 01:03 UTC](https://developer.community.nhs.uk/t/why-am-i-getting-this-error-when-trying-to-auth/668/2 "2024-01-14T01:03:12Z")

</div>

can you double check your sub and iss, it should be the api key not the application id

in portal

## Security details

Active API keys - and then

## Active keys

Key

to rule out code issues I would firstly create the jwt via something like this

> **[JWT tool](https://dinochiesa.github.io/jwt/)**
>
> An online tool to perform JWT verification, decoding, or generation. RSA keys and algorithms only.

and then run your tests against int environment in postman

---

<div class="post-metadata">

**Author:** ![hameed](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@hameed](https://developer.community.nhs.uk/u/hameed)\
**Post date:** [14 January 2024 19:19 UTC](https://developer.community.nhs.uk/t/why-am-i-getting-this-error-when-trying-to-auth/668/3 "2024-01-14T19:19:34Z")

</div>

@NHS england

can we make this clear in the API documentation, it trips alot of people up

the naming convention isnt in line with industry standards imo

---

<div class="post-metadata">

**Author:** ![ernest.kissiedu1](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@ernest.kissiedu1](https://developer.community.nhs.uk/u/ernest.kissiedu1)\
**Post date:** [15 January 2024 14:11 UTC](https://developer.community.nhs.uk/t/why-am-i-getting-this-error-when-trying-to-auth/668/4 "2024-01-15T14:11:33Z")

</div>

Hi @hameed

Thanks for the feedback. We will raise a ticket to investigate improving the documentation.

Regards,  
Ernest
