# Id\_token valid but returns "Unable to parse authentication token."

**URL:** <https://community.auth0.com/t/id-token-valid-but-returns-unable-to-parse-authentication-token/76323>\
**Category:** Get Help\
**Tags:** jwt\
**Created:** [January 13, 2022, 6:40am UTC](https://community.auth0.com/t/id-token-valid-but-returns-unable-to-parse-authentication-token/76323 "2022-01-13T06:40:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shanegohwenhan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/shanegohwenhan/32/34379_2.png) [@shanegohwenhan](https://community.auth0.com/u/shanegohwenhan)\
**Post date:** [January 13, 2022, 6:40am UTC](https://community.auth0.com/t/id-token-valid-but-returns-unable-to-parse-authentication-token/76323/1 "2022-01-13T06:40:06Z")

</div>

Hi everyone,

I am trying to validate the id token in my flask application using the code snippet here [Auth0 Python API SDK Quickstarts: Authorization](https://auth0.com/docs/quickstart/backend/python/01-authorization). The code below works for my localhost environment server but when I deployed it to Nginx, it somehow broke. However, access token validates perfectly fine.

Here’s my sample API call:  
[https://sampledomain/test](https://sampledomain/test)

Flask:

```auto
@app.route("/test")
@cross_origin(origin='localhost', headers=['Content-Type','IdToken'])
@requires_id_token
def test():
   return "working"

```

```auto
def get_id_token_auth_header():
    """Obtains the idToken from the idtoken Header
    """
    auth = request.headers.get("IdToken", None)
    if not auth:
        raise AuthError({"code": "id_token_header_missing",
                        "description":
                            "id_token header is expected"}, 401)
    parts = auth.split()
    if parts[0].lower() != "bearer":
        raise AuthError({"code": "invalid_header",
                        "description":
                            "id_token header must start with"
                            " Bearer"}, 401)
    elif len(parts) == 1:
        raise AuthError({"code": "invalid_header",
                        "description": "id_token not found"}, 401)
    elif len(parts) > 2:
        raise AuthError({"code": "invalid_header",
                        "description":
                            "id_token header must be"
                            " Bearer token"}, 401)
    id_token = parts[1]
    return id_token

```

In this algorithm, everything is the same except for the ‘audience’ which is the Client Id of my auth0

```auto
def requires_id_token(f):
    """Determines if the id Token is valid
    """
    @wraps(f)
    def decorated(*args, **kwargs):
        token = get_id_token_auth_header()
        jsonurl = urlopen("https://"+AUTH0_DOMAIN+"/.well-known/jwks.json")
        jwks = json.loads(jsonurl.read())
        unverified_header = jwt.get_unverified_header(token)
        rsa_key = {}
        for key in jwks["keys"]:
            if key["kid"] == unverified_header["kid"]:
                rsa_key = {
                    "kty": key["kty"],
                    "kid": key["kid"],
                    "use": key["use"],
                    "n": key["n"],
                    "e": key["e"]
                }
        if rsa_key:
            try:
                payload = jwt.decode(
                    token,
                    rsa_key,
                    algorithms=ALGORITHMS,
                    audience=< CLIENT ID HERE >,
                    issuer="https://"+AUTH0_DOMAIN+"/"
                )
            except jwt.ExpiredSignatureError:
                raise AuthError({"code": "token_expired",
                                "description": "token is expired"}, 401)
            except jwt.JWTClaimsError:
                raise AuthError({"code": "invalid_claims",
                                "description":
                                    "incorrect claims,"
                                    "please check the audience and issuer"}, 401)
            except Exception:
                raise AuthError({"code": "invalid_header",
                                "description":
                                    "Unable to parse authentication"
                                    " token."}, 401)

            _request_ctx_stack.top.current_user = payload
            return f(*args, **kwargs)
        raise AuthError({"code": "invalid_header",
                        "description": "Unable to find appropriate key"}, 401)
    return decorated

```

However, when I tried either my own application or postman, I receive error 401,

```auto
{
    "code": "invalid_header",
    "description": "Unable to parse authentication token."
}

```

I verified the id\_token on [jwt.io](http://jwt.io) and it is valid.

May I know what might be the issue?

PS: Didn’t include authorization because it is working.

---

<div class="post-metadata">

**Author:** ![john.gateley](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/john.gateley/32/10879_2.png) [@john.gateley](https://community.auth0.com/u/john.gateley)\
**Post date:** [January 13, 2022, 5:39pm UTC](https://community.auth0.com/t/id-token-valid-but-returns-unable-to-parse-authentication-token/76323/2 "2022-01-13T17:39:53Z")

</div>

Hi @shanegohwenhan

For an API call you should be using an Access Token, not an ID token.  
Your get\_id\_token\_auth\_header function has the comment “Obtains the Access token”.

I haven’t looked to closely at your code, but wouldn’t be surprised if you are looking for an access token field in the ID token.

Rewrite it so that it is using an access token, and make sure you are passing an access token.

John

---

<div class="post-metadata">

**Author:** ![shanegohwenhan](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/shanegohwenhan/32/34379_2.png) [@shanegohwenhan](https://community.auth0.com/u/shanegohwenhan)\
**Post date:** [January 14, 2022, 12:46am UTC](https://community.auth0.com/t/id-token-valid-but-returns-unable-to-parse-authentication-token/76323/3 "2022-01-14T00:46:43Z")

</div>

Hi John, thanks for the reply.

I had added additional rules to the access token which includes the email in it. It solved my issue and I did not need the id token anymore.  
Also, I found out that the issue is not with the claims themselves, however, it’s with the .env file that wasn’t specified properly in my .ini file in wsgi, causing an issue when I tried to set the email attribute to my flask session variable. It’s all working fine now!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [January 29, 2022, 12:47am UTC](https://community.auth0.com/t/id-token-valid-but-returns-unable-to-parse-authentication-token/76323/4 "2022-01-29T00:47:29Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
