如何将帐户与 Django rest auth / all auth 连接?

How to connect accounts with Django rest auth / all auth?

我正在使用 Django rest-auth 通过 Django rest 框架进行身份验证和帐户创建。目前,我设置了六个提供商,我想开始连接它们。

文档(rest-auth 或 all-auth)并不清楚我必须做什么才能连接帐户。

例如,如果我尝试将 Google 帐户连接到 LinkedIn 帐户(相同的电子邮件地址),即使我使用正确的 /rest-auth/linkedin/connect/ 向 POST 发出请求Google 提供商的访问令牌,我收到 400 响应:"non-field-errors": [ "Incorrect value" ]

我正在使用 JWT 进行身份验证,并在授权中提供正确的令牌 header。

views.py

class GoogleConnect(SocialConnectView):
    adapter_class = GoogleOAuth2Adapter

class LinkedInConnect(SocialConnectView):
    adapter_class = LinkedInOAuth2Adapter

我已经编写了一个教程,应该可以解决您的问题。 This应该是你要找的部分。

简而言之:您必须编写自己的 ConnectView,它继承自 rest_auth 视图,如下所示:

class GoogleCallbackConnect(SocialConnectView):
    """
    Connects the provider's user account to the currently logged in user.
    """
    adapter_class = GoogleOAuth2Adapter
    client_class = OAuth2Client

    @property
    def callback_url(self):
        url = self.adapter_class(self.request).get_callback_url(
            self.request,
            None,
        )
        return url