1

In my Blazor Server app, the user authenticated using openid, and then in the app, I display data based on calling some external APIs using HttpClient and setting the "Authorization" header with the access token obtained from the login.

But I will also need to handle any unauthorized response from calling the external APIs with HttpClient.

Is there a better/simpler way?

2
  • Unauthorized response from the openid login or from a call within the blazor server-side code using HttpClient? I'd assume you mean the HttpClient calls? Commented Aug 10, 2022 at 19:45
  • @keithwill-the-upvoter Unauthorized response from calling the external APIs with HttpClient. Commented Aug 10, 2022 at 20:06

1 Answer 1

1

Is there a better/simpler way?

Not generically. How you handle API errors depends on the calling conventions of the API and how you will incorporate the results into your application. Some APIs return Unauthorized when the Bearer token has expired, signaling you to use your refresh token to get a new access token. In some endpoints Unauthorized might just mean the user doesn't have the appropriate role or permissions.

The answer is "yes, you need to handle unauthorized responses" and you'll need to do it in a way that is appropriate for the API you are calling in the context of the place you are integrating it into your app. Sometimes that means you can use a wrapper which will perform some generic action on unauthorized or other errors, but the only one that can make that determination is the dev that is familiar enough with both while integrating with the API and writing the access code for their app.

Some of that advice changes if you are trying to handle transient errors, but you specifically asked about unauthorized responses.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.