Fix unauthorized request handling. Thanks @iwanbk for providing the patch.