我是 Django 和社交身份验证的新手。
我正在尝试制作一个人们登录(使用谷歌)并发布内容的网站。我正在使用 python-social-auth 并且用户登录得很好。然后我想使用 DRF,它需要它自己的身份验证,所以我查看了 django-rest-framework-social-auth,但不明白如何使用它。我按照说明更改了我的 settings.py 并且用户仍在进行身份验证。但不适用于 DRF(发布请求响应 401 错误)。据我了解,我需要向客户端发送一个令牌,他必须将其包含在后续请求中。
我有以下疑问:
在 https://github.com/PhilipGarnero/django-rest-framework-social-oauth2 中,有一个获取令牌的 curl 请求(我认为它是 access_token),我应该在哪里做要求?因为它包含客户端机密,我认为它不能在浏览器中?(我仍然尝试 curl 请求,它显示“无效客户端”)我也直接在 python-social-auth 管道中获取访问令牌。如何将其转换为用户访问令牌(在后端)?
有一个关于社会认证类的部分,但我不知道如何使用它。
我的看法:
class WantedViewSet(viewsets.ModelViewSet):
queryset = models.Wanted.objects.all()
serializer_class = serializers.WantedSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
authentication_classes=(SocialAuthentication,OAuth2Authentication,)
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
Run Code Online (Sandbox Code Playgroud)
设置:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.ext.rest_framework.OAuth2Authentication',
'rest_framework_social_oauth2.authentication.SocialAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
),
}
Run Code Online (Sandbox Code Playgroud) authentication oauth django-rest-framework python-social-auth