1

我已经阅读了有关如何集成 REST 框架和 OAuth 工具包的文档,但是当我使用令牌运行请求时,我收到:

{"detail":"Authentication credentials were not provided."}

这是我的 REST 设置:

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
  'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
  'rest_framework.authentication.SessionAuthentication',
  'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
'PAGE_SIZE': 20

}

示例视图:

from django.contrib.auth.models import Group

from rest_framework import permissions, viewsets
from oauth2_provider.contrib.rest_framework import TokenHasScope, OAuth2Authentication

from data_commons_security.security.models import Permission, Profile, Role, User
from data_commons_security.security.serializers import (
    GroupSerializer,
)


class GroupViewSet(viewsets.ReadOnlyModelViewSet):
    authentication_classes = [OAuth2Authentication]
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
    permission_classes = [TokenHasScope]
    required_scopes = ['read', 'groups']

而且我的 INSTALLED_APPS 中确实有“oauth2_provider”。我可以毫无问题地请求和接收令牌。只有当我使用它们时,我才会收到上述错误。

4

0 回答 0