I am trying to access the azure api for fhir from a react application. I am using msal-react 2.x. I get a 401 error when I try to access the "/Patient" rest endpoint. I have created a Azure AD application (SPA). The app has FHIR Data Reader access to the fhir api service. Api permissions are
Azure Healthcare Apis (patient.all.read, user_impersonation).
Graph (User.Read, profile, openid).
CORS is enabled on the FHIR api.
React code. index.js
<MsalProvider instance={msalInstance}>
<App />
</MsalProvider>
App.js
<MsalAuthenticationTemplate
interactionType={InteractionType.Popup}
authenticationRequest={authRequest}
errorComponent={ErrorComponent}
loadingComponent={LoadingComponent}
>
<App />
</MsalAuthenticationTemplate>
msalConfig.js
const msalConfig = {
auth: {
clientId: "{clientid}",
authority: 'https://login.microsoftonline.com/{tenantid}',
},
};
export const msalInstance = new PublicClientApplication(msalConfig);
export const authRequest = {
scopes: [
"https://{myserver}.azurehealthcareapis.com/.default"
]
};
export const acquireAccessToken = async () => {
const activeAccount = msalInstance.getActiveAccount();
const accounts = msalInstance.getAllAccounts();
const request = {
scopes: [
"https://{myserver}.azurehealthcareapis.com/.default"
],
account: activeAccount || accounts[0]
};
const authResult = await msalInstance.acquireTokenSilent(request);
return authResult.accessToken
};
I am probably missing something with the scopes.

I registered one Azure AD application and added API permissions as below:
When I tried to access the "/Patient" rest endpoint with token generated with
User.Readscope, I too got 401 Unauthorized error as below:Response:
To confirm that, you can decode the access token by pasting it in jwt.ms and check
aud&scpclaims:To resolve the error, make sure to pass only Azure Healthcare API in
scopeparameter while generating access token.In my case, I generated access token using authorization code flow via Postman with below parameters:
Response:
To check
audandscpclaims, you can decode above token by pasting it in jwt.ms website:When I used this token to access the "/Patient" rest endpoint, I got response successfully as below:
Response:
In your code, remove all Microsoft Graph permissions like User.Read, openid, profile from
scopeparameter and pass only Azure Healthcare API scope like this:If you are generating access token by authenticating as user, make sure to assign
FHIR Data Readerrole to user account under FHIR API service like this: