python - Django-rest-framework 与 django OAuth 2.0 提供身份验证错误

标签 python django oauth django-rest-framework django-oauth

我已经将 django-rest-framework 与 django-oauth-toolkit 集成在一起。它给我 {"detail": "Authentication credentials were not provided."} 和未验证的 api。

这是我的设置.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    )
}

View .py

from rest_framework.views import APIView
from rest_framework.response import Response


class SignUpView(APIView):
    """
        Signup for the user.
    """
    def get(self, request):
        return Response({'result': True, 'message': 'User registered successfully.'})

网址.py

from django.urls import path
from myapp.views import SignUpView

urlpatterns = [
    path('signup/', SignUpView.as_view()),

]

最佳答案

对于注册用户,您不需要任何身份验证。所以你需要这样写你的 View 。

class SignUpView(APIView):
    """
        Signup for the user.
    """
    authentication_classes = ()
    permission_classes = ()

    def get(self, request):
        return Response({'result': True, 'message': 'User registered successfully.'})

对于所有其他请求,您需要在 header 中传递身份验证 token 。在这种情况下,您无需提及身份验证和权限类,因为将使用您的默认类。

关于python - Django-rest-framework 与 django OAuth 2.0 提供身份验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48772596/

相关文章:

python - 当我不想复制父类中的每个方法时,如何为 Python 子类中的每个方法添加延迟

python - 在 Django 1.7 中使用 html 发送电子邮件

python - 如何将 Tkinter 安装到 Aptana Studio 35-32(非 Eclipse)

python - 导入 pyodbc 时出错

django - 如何创建涉及使用 @property 方法进行计算的 POST 方法

OAuth Instagram API 在未登录浏览器的情况下连接 Instagram 时删除重定向 URL 中的参数

asp.net webapi UseOAuthBearerAuthentication 与 UseJwtBearerAuthentication

c# - Twitter 流 API C#

python - QThread:在Python中线程仍在运行时被销毁

python - Django 查询 : Getting data over two relations