I am trying to retrieve an image from Azure blob, but I am getting a 403 response with the following message,
"403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature."
Following is the code I used to retrieve the image
public getBase64Image(): Observable<string>{
return this.http.get(environment.receiptLogoUrl, { responseType: 'blob' })
.pipe(
switchMap((blob: Blob) => {
return new Observable<string>((observer) => {
const reader = new FileReader();
reader.readAsBinaryString(blob);
reader.onloadend = () => {
const base64data = btoa(reader.result as string);
observer.next(`data:image/png;base64,${base64data}`);
observer.complete();
};
});
})
);
}
How should this issue be fixed?
2
Answers
The error was caused since the token interceptor has added an authorization header, which fails the request to the Azure blob. To avoid this one workaround is given in the following article,
https://medium.com/nerd-for-tech/how-to-bypass-angular-http-interceptor-2491afca16a3#:~:text=Interceptors%20sit%20between%20the%20HttpClient,a%20new%20HttpClient%20using%20HttpBackend%20.&text=When%20injected%2C%20HttpBackend%20dispatches%20requests,going%20through%20the%20interceptor%20chain.
The error says the authentication is failing, so you need to authenticate the request. Here are the two main ways to provide authentication for Azure blobs:
First check the Azure portal to see which authentication method is currently set. Then follow the appropriate instructions on how to authenticate with that method. There are more details here:
https://learn.microsoft.com/en-us/azure/storage/blobs/authorize-data-operations-portal#navigate-to-blobs-in-the-azure-portal
If you’re using AD, AAD, MSA, sign in with google/facebook/linkedin/MS, etc, there is an Angular library that can help with this:
https://learn.microsoft.com/en-us/azure/active-directory-b2c/configure-authentication-sample-angular-spa-app