# Random 401 response with "invalid\_header: unable to find appropriate key" when calling python flask API

**URL:** <https://community.auth0.com/t/random-401-response-with-invalid-header-unable-to-find-appropriate-key-when-calling-python-flask-api/81664>\
**Category:** JWT.io\
**Tags:** jwt, api, auth0\
**Created:** [April 13, 2022, 7:10am UTC](https://community.auth0.com/t/random-401-response-with-invalid-header-unable-to-find-appropriate-key-when-calling-python-flask-api/81664 "2022-04-13T07:10:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/chu/32/33549_2.png) [@chu](https://community.auth0.com/u/chu)\
**Post date:** [April 13, 2022, 7:10am UTC](https://community.auth0.com/t/random-401-response-with-invalid-header-unable-to-find-appropriate-key-when-calling-python-flask-api/81664/1 "2022-04-13T07:10:18Z")

</div>

Hi,  
I have been struggling a lot with Auth0 with random 401 response when calling either auth0 apis or my own python flask API. This has been lasting for a long time.

My environment:  
Nodejs 16, Nextjs 12.1.4, Nextjs-auth0 1.7.0  
Authenticate using SSR Nextjs paradigm  
Docker, Azure web app service  
Cloudflare as my DNS provider

Auth0 webapp settinig:  
Regular Web Application

In the same endpoint calls, the endpoint calls fail from time to time:

 ![image](https://global.discourse-cdn.com/auth0/original/3X/c/8/c88cf273b94ff116cd9b67c559804d52b8facc93.png)  
 ![image](https://global.discourse-cdn.com/auth0/original/3X/2/b/2b01e2bd64fbd20b5d7957cbf971adecc7c47f0c.png)  
The response in python is:  
invalid\_header: unable to find appropriate key

 ![image](https://global.discourse-cdn.com/auth0/original/3X/2/a/2a17c851f99d442b5dcc28e5cc69243e8369b21b.png)  
The accesstoken sometimes does not have the right RSA key (kid missing).

This happens to almost all endpoints, including other examples provided by auth0 sdk example:  
/api/auth/me  
/api/auth/profile  
The weird thing is that in my development deployment, this does not happen

Does anyone know what’s going on with this?

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [April 14, 2022, 12:41am UTC](https://community.auth0.com/t/random-401-response-with-invalid-header-unable-to-find-appropriate-key-when-calling-python-flask-api/81664/3 "2022-04-14T00:41:13Z")

</div>

Hi @chu,

Welcome to the Auth0 Community!

I understand that you have encountered intermittent errors when authenticating against your APIs.

First, could you please decode the access token from the response using [jwt.io](http://jwt.io) to see if the access token has a valid signature?

And could you please clarify which Auth0 SDKs did you test that you found the same issue with almost all endpoints?

I would like to see if I can reproduce this issue.

Thank you.

---

<div class="post-metadata">

**Author:** ![chu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/chu/32/33549_2.png) [@chu](https://community.auth0.com/u/chu)\
**Post date:** [April 14, 2022, 12:44pm UTC](https://community.auth0.com/t/random-401-response-with-invalid-header-unable-to-find-appropriate-key-when-calling-python-flask-api/81664/4 "2022-04-14T12:44:08Z")

</div>

Thanks for reply. Will come back shortly with data.

---

<div class="post-metadata">

**Author:** ![chu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/chu/32/33549_2.png) [@chu](https://community.auth0.com/u/chu)\
**Post date:** [April 19, 2022, 12:07am UTC](https://community.auth0.com/t/random-401-response-with-invalid-header-unable-to-find-appropriate-key-when-calling-python-flask-api/81664/5 "2022-04-19T00:07:29Z")

</div>

Hi @rueben.tiow ,

The access tokens are identical for both failing and ok API calls and the signature is verified using [jwt.io](http://jwt.io). auth0-python==3.13.0 is SDK used in Flask. It is happening in almost all endpoints from my side and I do not see a clear pattern when it shall happen.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [April 21, 2022, 9:09pm UTC](https://community.auth0.com/t/random-401-response-with-invalid-header-unable-to-find-appropriate-key-when-calling-python-flask-api/81664/6 "2022-04-21T21:09:36Z")

</div>

Hi @chu,

Thank you for your response.

> [@chu](#):
>
> The access tokens are identical for both failing and ok API calls and the signature is verified using [jwt.io](http://jwt.io)

In this case, could you please [capture a HAR file](https://auth0.com/docs/troubleshoot/troubleshooting-tools/generate-and-analyze-har-files) of the authentication events, including the _Invalid\_header_ error and DM it to me?

I would like to investigate your observations further.

Thank you.

---

<div class="post-metadata">

**Author:** ![chu](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/chu/32/33549_2.png) [@chu](https://community.auth0.com/u/chu)\
**Post date:** [June 7, 2022, 8:07am UTC](https://community.auth0.com/t/random-401-response-with-invalid-header-unable-to-find-appropriate-key-when-calling-python-flask-api/81664/7 "2022-06-07T08:07:59Z")

</div>

By adding a proper scope, the problem disappeared even though I am not using the scope. Thanks for the help from @rueben.tiow

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 21, 2022, 3:43pm UTC](https://community.auth0.com/t/random-401-response-with-invalid-header-unable-to-find-appropriate-key-when-calling-python-flask-api/81664/8 "2022-12-21T15:43:46Z")

</div>


