from rest_framework.exceptions import AuthenticationFailed from rest_framework.permissions import IsAuthenticated from rest_framework.views import APIView from apps.api.authentication import ApiKeyAuthentication from apps.api.errors import api_error class ExternalApiView(APIView): authentication_classes = (ApiKeyAuthentication,) permission_classes = (IsAuthenticated,) def permission_denied(self, request, message=None, code=None): if request.authenticators and not request.successful_authenticator: raise AuthenticationFailed(api_error("unauthorized", "缺失或无效 API Key")) super().permission_denied(request, message=message, code=code)