3

在过去的 2 个月中,我使用 python social auth 进行社交身份验证,这很棒。我需要 QQ 支持,因此安装了最新的 git commit(23e4e289ec426732324af106c7c2e24efea34aeb - 不是发布的一部分)。到目前为止,我曾经使用以下代码对用户进行身份验证:

    # setup redirect uri in order to load strategy
    uri = redirect_uri = "social:complete"
    if uri and not uri.startswith('/'):
        uri = reverse(redirect_uri, args=(backend,))

    # load the strategy
    try:
        strategy = load_strategy(
            request=request, backend=backend,
            redirect_uri=uri, **kwargs
        )
        strategy = load_strategy(request=bundle.request)
    except MissingBackend:
        raise ImmediateHttpResponse(HttpNotFound('Backend not found'))

    # get the backend for the strategy
    backend = strategy.backend

    # check backend type and set token accordingly
    if isinstance(backend, BaseOAuth1):
        token = {
            'oauth_token': bundle.data.get('access_token'),
            'oauth_token_secret': bundle.data.get('access_token_secret'),
        }
    elif isinstance(backend, BaseOAuth2):
        token = bundle.data.get('access_token')
    else:
        raise ImmediateHttpResponse(HttpBadRequest('Wrong backend type'))

    # authenticate the user
    user = strategy.backend.do_auth(token)

效果很好。

在最新版本中,此行为已更改,并且由于“load_strategy”方法已更改而引发异常。

我似乎找不到任何关于如何使用新版本的文档。

任何帮助,将不胜感激!

暗里。

4

1 回答 1

4

存储库中的最后一次更改改变了 的重要性strategy,而不是作为执行身份验证的主要实体,它只是一个帮助类将框架与后端粘合在一起。尝试使用此代码段加载strategybackend

from social.apps.django_app.utils import load_strategy, load_backend

strategy = load_strategy(request)
backend = load_backend(strategy, backend, uri)
...
user = backend.do_auth(token)
于 2014-06-22T16:21:06.503 回答