【问题标题】:Flutter + Django OAuth integrationFlutter + Django OAuth 集成
【发布时间】:2020-07-04 13:44:31
【问题描述】:

我使用 Flutter 作为前端,使用 Django 作为后端。我正在尝试将 Google 和 Facebook OAuth 集成到应用程序中,并使用一些颤振的图书馆,我能够在前端获取用户详细信息和访问令牌。现在的问题是我如何处理用户并为他们访问令牌并通过 drf 验证他们。我可以完全依赖 drf 进行 OAuth,并在前端使用 Django 的 OAuth toolikt 使用 http 请求创建用户,但是有没有一种方法可以在前端处理传入的身份验证令牌并在 drf 中验证它们以便在后端注册它们。

【问题讨论】:

    标签: flutter django-rest-framework oauth


    【解决方案1】:
    @api_view(http_method_names=['POST'])
    @permission_classes([AllowAny])
    @psa()
    def exchange_token(request, backend):
        serializer = SocialSerializer(data=request.data)
    
        if serializer.is_valid(raise_exception=True):
            # This is the key line of code: with the @psa() decorator above,
            # it engages the PSA machinery to perform whatever social authentication
            # steps are configured in your SOCIAL_AUTH_PIPELINE. At the end, it either
            # hands you a populated User model of whatever type you've configured in
            # your project, or None.
            user = request.backend.do_auth(serializer.validated_data['access_token'])
    
            if user:
                # if using some other token back-end than DRF's built-in TokenAuthentication,
                # you'll need to customize this to get an appropriate token object
                token, _ = Token.objects.get_or_create(user=user)
                return Response({'token': token.key})
    
            else:
                return Response(
                    {'errors': {'token': 'Invalid token'}},
                    status=status.HTTP_400_BAD_REQUEST,
                )
    

    在您的设置(完整代码)中只需要添加一点内容,然后就完成了:

    AUTHENTICATION_BACKENDS = (
        'social_core.backends.google.GoogleOAuth2',
        'social_core.backends.facebook.FacebookOAuth2',
        'django.contrib.auth.backends.ModelBackend',
    )
    for key in ['GOOGLE_OAUTH2_KEY',
                'GOOGLE_OAUTH2_SECRET',
                'FACEBOOK_KEY',
                'FACEBOOK_SECRET']:
        # Use exec instead of eval here because we're not just trying to evaluate a dynamic value here;
        # we're setting a module attribute whose name varies.
        exec("SOCIAL_AUTH_{key} = os.environ.get('{key}')".format(key=key))
    SOCIAL_AUTH_PIPELINE = (
      'social_core.pipeline.social_auth.social_details',
      'social_core.pipeline.social_auth.social_uid',
      'social_core.pipeline.social_auth.auth_allowed',
      'social_core.pipeline.social_auth.social_user',
      'social_core.pipeline.user.get_username',
      'social_core.pipeline.social_auth.associate_by_email',
      'social_core.pipeline.user.create_user',
      'social_core.pipeline.social_auth.associate_user',
      'social_core.pipeline.social_auth.load_extra_data',
      'social_core.pipeline.user.user_details',
    )
    

    在您的 urls.py 中添加到此函数的映射,一切就绪!

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 2018-04-28
      • 2011-04-12
      • 2021-02-18
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 2019-07-26
      相关资源
      最近更新 更多