我如何使用 python-social-auth 和 django-graphql-auth return 刷新令牌?

How can I return the refresh token using python-social-auth and django-graphql-auth?

如何在使用社交身份验证时return刷新令牌?我试过这个解决方案 我不知道如何让它与 graphql 兼容。

这是我的尝试:

import graphql_social_auth
from graphql_jwt.shortcuts import get_token


class SocialAuth(graphql_social_auth.SocialAuthJWT):
    refresh_token = graphene.String()
    token = graphene.String()

    @classmethod
    def resolve(cls, root, info, social, **kwargs):
        return cls(
            user=social.user,
            token=get_token(social.user),
            refresh_token=social.extra_data['refresh_token']
        )

当我注释掉刷新令牌时,用户和令牌字段没问题。就是不知道刷新令牌从哪弄,好像不在extra_data.

我已经处理这个问题好几个小时了,所有的搜索结果都没有帮助。非常感谢任何指导。

你不需要在扩展 SocialAuthJWT 时再次声明 token 字段,因为它已经有一个来自 JSONWebTokenMixin mixin 的 token 字段,除非你需要用不同类型。

关于刷新令牌,您可以通过以下方式获取:

import graphql_social_auth
from graphql_jwt.shortcuts import get_token
from graphql_jwt.shortcuts import create_refresh_token


class SocialAuth(graphql_social_auth.SocialAuthJWT):
    refresh_token = graphene.String()

    @classmethod
    def resolve(cls, root, info, social, **kwargs):
        if social.user.refresh_tokens.count() >= 1:
            refresh_token = social.user.refresh_tokens.last()
        else:
            refresh_token = create_refresh_token(social.user)

        return cls(
            user=social.user,
            token=get_token(social.user),
            refresh_token=refresh_token
        )