2

在安装和设置了 social-auth 之后,我正在玩一会儿,试图掌握它。我已阅读文档,并已使用示例项目使其运行。

但直到现在我不知道如何获取某个提供商的信息。在示例项目中,模板标签总是以这种方式使用:

{% for type, accounts in social_auth.associated.items %}
    {% for account in accounts %}
        {{account.provider}} is connected.
    {% endfor %}
{% endfor %}

我现在要做的不是列出所有提供商,而是检查是否有人将他的帐户连接到(即)facebook。这样我就可以做这样的事情:

if user==connected_to_facebook
    provide some functionality
endif

从上面的示例中,我知道 social_auth.associated.items 包含 的元组(type,account),其中“facebook”将在一个包含所有值的列表中account.provider

我想到的是这样的:

{% if "facebook" in social_auth.associated.items.accounts.provider %}

显然,这是行不通的。我认为这个会起作用,但不会返回我想要的结果:

{% if "facebook" in social_auth.associated.items[1].provider %}

Django中是否有一些我可以使用的功能?也许我缺少一些特殊的模板标签?或者这个功能是否已经内置在 social_auth 中,我不知何故错过了文档?或者,我最怀疑的是,它真的很明显吗,我只是......

非常欢迎任何帮助。

4

2 回答 2

3

“social_auth”不是元组中的一些元组,它是一个字典:

{'not_associated': {}, 'backends': {'oauth2': ['facebook']},
 'associated': {'oauth2': [<UserSocialAuth:testuser>]}}

这当然更有意义,但仍然没有导致任何地方。因此,我查看了一个尚未关联其帐户的用户,该字典如下所示:

{'not_associated': {'oauth2': ['facebook']}, 'backends': {'oauth2': ['facebook']},
 'associated': {}}

现在我发现了一些有用的东西:

{% if "facebook" in social_auth.not_associated.oauth2 %}
{% else %}
    provide facebook functionality
{% endif %}

这样可行。您只需要知道您正在寻找的后端使用什么类型的身份验证,然后确保它不在 social_auth 的 not_associated 字段中。

于 2012-04-04T16:35:05.283 回答
0

如果有人需要创建断开连接的 URL,这是我想出的 hack-y 代码,providers我传入的列表在哪里["Facebook", "Twitter"]

{% for p in providers %}
    <h2>Link {{ p }} Account</h2>
    <p>Use your {{ p }} account to log in
    {% if p|lower in social_auth.not_associated %}
        <a href="{% url socialauth_associate_begin p|lower %}?next={{ request.path }}" class="off">No</a>
    {% else %}
        {% for item in social_auth.associated.all %}{% if item.provider == p|lower %}
        <a href="{% url socialauth_disconnect_individual p|lower item.id %}" class="on">Yes</a>
        {% endif %}{% endfor %}
    {% endif %}
    </p>
{% endfor %}

似乎应该有一个关联帐户的字典而不是这样做,但我想不通。不确定如果用户成功授权他们的帐户两次会发生什么。

于 2013-05-29T20:27:43.840 回答