【问题标题】:How to get the Refresh Token with the django-graphql-social-auth library如何使用 django-graphql-social-auth 库获取刷新令牌
【发布时间】:2021-08-24 21:07:40
【问题描述】:

您好,我正在使用 django-graphql-social-auth 库,每当我使用此突变创建社交用户时:

import graphene
import graphql_social_auth


class Mutations(graphene.ObjectType):
    social_auth = graphql_social_auth.SocialAuthJWT.Field()

还有这个 Graphql 突变:

mutation SocialAuth($provider: String!, $accessToken: String!) {
  socialAuth(provider: $provider, accessToken: $accessToken) {
    social {
      uid
    }  
    token
    refreshToken
    }
 }

我收到此错误:

    {
  "errors": [
    {
      "message": "Cannot query field \"refreshToken\" on type \"SocialAuthJWT\".",
      "locations": [
        {
          "line": 29,
          "column": 5
        }
      ]
    }
  ]
}

提前感谢您的帮助。

【问题讨论】:

  • 嗨@Roy,我只是想知道您的社交登录的用例。您是否仅将其用于帐户注册然后后端为其分配自己的令牌以进行 api 调用?还是您实际使用了各自平台的社交 API?
  • 嗨@TommyLeong 我已经解决了这个问题,现在它工作得非常酷,你可以查看我刚刚更新的答案。后端使用各自平台的社交 API,但这是一个单独的情况,我还需要 API 来提供各自的应用令牌(不是平台社交 API 的令牌),以便在我的应用和我的 API 之间创建令牌和刷新令牌系统。

标签: python django authentication jwt social


【解决方案1】:

我是这样解决的:

    # Social authentication
class SocialAuth(graphql_social_auth.SocialAuthMutation):
    token = graphene.String()
    refresh_token = graphene.String()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 2012-10-06
    • 2018-07-18
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多