Whenever the button is clicked, a 401 error code is returned and the logged in Admin is redirected to the Strapi Admin login page (session terminated).
Based on all the code samples I see, this should work. I’m logged in as a Super Admin user. I can also confirm that the same Bearer Token being used for my active session is being submitted as an authorization header with the request.
I ran into the same issue, I was able to solve it by not including ‘api/’ in my request url. Still unclear on why that worked, but might be worth a shot.
I know this is VERY late, but hopefully this helps the next dev.
The reason for this error is that Strapi has two separate API services, which isn’t documented well anywhere I could find.
There is an “Internal admin API” for plugin-specific or core admin api calls. You can use fetchRequest library because these calls are expected to be used by Admin Users.
There is also a “Content API/ External API” which is the APIs that are auto-generated for each content type as well as anything in the top level src/api folder. These are intended to be used by end users, and have a separate auth JWT token system.
Calling an external API with an Admin User JWT (which is what useFetchClient uses) will result in a 401 because the JWT is invalid for that API’s token service.
As for how to fix this, I have been moving my APIs into a plugin (I just made a “common” plugin), which allows for Admin User access. You could also allow public access to the API in question and use fetch instead of useFetchClient if access isn’t an issue.