# Getting 401 Unauthorized even though token should be valid

**URL:** <https://devforum.okta.com/t/getting-401-unauthorized-even-though-token-should-be-valid/3135>\
**Category:** OAuth/OIDC\
**Created:** [October 18, 2018, 6:54pm UTC](https://devforum.okta.com/t/getting-401-unauthorized-even-though-token-should-be-valid/3135 "2018-10-18T18:54:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jabber30](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@jabber30](https://devforum.okta.com/u/jabber30)\
**Post date:** [October 18, 2018, 6:54pm UTC](https://devforum.okta.com/t/getting-401-unauthorized-even-though-token-should-be-valid/3135/1 "2018-10-18T18:54:53Z")

</div>

I have integrated my apps with Okta and App-A (through which I get token from Okta when user signs in) is calling API in App-B by passing the token.

This works for initial 20-30 minutes (token expiration is in 1 hour) but then I start getting 401 Unauthorized error which will only go away if the user logs out and logs back in.

Note: These are C# web apps.

Any idea what could be the issue?

---

<div class="post-metadata">

**Author:** ![jmelberg](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/jmelberg/32/154_2.png) [@jmelberg](https://devforum.okta.com/u/jmelberg)\
**Post date:** [October 22, 2018, 8:51pm UTC](https://devforum.okta.com/t/getting-401-unauthorized-even-though-token-should-be-valid/3135/2 "2018-10-22T20:51:48Z")

</div>

Hi @jabber30,

This sounds like a possible clock-drift issue, where the system clock is drifting ahead by approximately 30 minutes. You should be receiving an error message with the `401` response code. Can you share that with us so we can help identify the root cause?

---

<div class="post-metadata">

**Author:** ![jabber30](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@jabber30](https://devforum.okta.com/u/jabber30)\
**Post date:** [October 22, 2018, 10:07pm UTC](https://devforum.okta.com/t/getting-401-unauthorized-even-though-token-should-be-valid/3135/3 "2018-10-22T22:07:21Z")

</div>

Hi @jmelberg  
Thanks for your response. Actually, the reason was that if I try to refresh token AFTER it has expired, it returns the new token, but the state is ACTIVE : false. So I guess I should always try to refresh token before the original token’s expired.

Btw is that really true that you can’t refresh a token after it has expired?

Jay

---

<div class="post-metadata">

**Author:** ![jmelberg](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/jmelberg/32/154_2.png) [@jmelberg](https://devforum.okta.com/u/jmelberg)\
**Post date:** [October 22, 2018, 11:17pm UTC](https://devforum.okta.com/t/getting-401-unauthorized-even-though-token-should-be-valid/3135/4 "2018-10-22T23:17:22Z")

</div>

@jabber30,

Tokens can be refreshed assuming you have one of the following:

1. A `refreshToken` to pass to the `/token` endpoint
  - Using a long-lived `refreshToken` will allow you to mint new access and identity tokens when they expire. See [refreshing tokens](https://developer.okta.com/authentication-guide/tokens/refreshing-tokens) for more information.

2. A valid SSO session within Okta
  - Depending on the OAuth grant you’re using (authorization code, implicit, or hybrid), you can leverage a user’s Okta session to mint new tokens by redirecting to the `/authorize` endpoint. Once the user’s session has expired, you’ll need to prompt for login again as tokens as this _silent login flow_ will fail.

Can you double check your implementation to ensure you have a valid `refreshToken` or Okta session before making the request?

---

<div class="post-metadata">

**Author:** ![jabber30](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@jabber30](https://devforum.okta.com/u/jabber30)\
**Post date:** [October 23, 2018, 4:03pm UTC](https://devforum.okta.com/t/getting-401-unauthorized-even-though-token-should-be-valid/3135/5 "2018-10-23T16:03:51Z")

</div>

Hi @jmelberg

I think there has been some misunderstanding. I am able to refresh token as long as I refresh them before the token has expired. And I think your statement agrees with me _ **" 1. \* Once the user’s session has expired, you’ll need to prompt for login again as tokens as this silent login flow will fail."** _

---

<div class="post-metadata">

**Author:** ![jabber30](https://avatars.discourse-cdn.com/v4/letter/j/bc79bd/32.png) [@jabber30](https://devforum.okta.com/u/jabber30)\
**Post date:** [October 24, 2018, 12:39am UTC](https://devforum.okta.com/t/getting-401-unauthorized-even-though-token-should-be-valid/3135/6 "2018-10-24T00:39:11Z")

</div>

Hi @jmelberg  
I am actually having a different issue right now. Before I state that, here is my settings:  
OAuthResponseType = code id\_token  
OAuthScopes = openid email profile address phone groups offline\_access

At the launch of my app, the user is prompted with Okta login screen upon which I get the access token, refresh token and such. Once that is done, I keep monitoring the expiry time of the token, and 15 minutes before it is about to expire, I refresh the token.

What I found is that the refresh token logic works and it extends the expiry time by an hour, but about 5-15 minutes after getting the refreshed token, the ACTIVE status of the token starts returning false when I do the introspection with Okta.

Is there something I am missing? Did I provide enough information?

Thanks in advance,  
Jay

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/oktadev/original/1X/589c4c5f80de422d01f47cee7185a7fb8227835c.png) [@system](https://devforum.okta.com/u/system)\
**Post date:** [January 12, 2024, 11:53pm UTC](https://devforum.okta.com/t/getting-401-unauthorized-even-though-token-should-be-valid/3135/7 "2024-01-12T23:53:57Z")

</div>

Closed on January 12, 2024, 11:53pm UTC

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