Django-allauth Linkedin OAUTH2.0在extra_data中缺少字段

时间:2015-12-14 04:29:18

标签: python django django-allauth

所有其他配置导致登录失败(SERVER 500 ERROR)。在十几种不同组合的排列之后,唯一具有登录成功但在仍然遗漏了extra_data中的字段的配置如下。缺少的字段可以是'技能','r_basicprofile'范围内的'summary'。

我错过了一些导入信息吗?如果你发现它,请告诉我。我一直在阅读django-allauth的源代码来理解这个问题。

实际上,这里有两个问题:

  1. 使用linkedin_oauth2的正确配置是什么?完整配置包括login.html,INSTALLED_APPS和SOCIALACCOUNT_PROVIDERS。
  2. 为什么PROFILE_FIELDS中指定的字段未填充到extra_data?
  3. 我的Linkedin社交登录应用程序最近创建。那么,它是使用OAuth2.0而不是OAuth1.0,对吗?我按照文档https://django-allauth.readthedocs.org/en/latest/providers.html?highlight=provider_login_url#linkedin上的说明进行操作,该文档说“将OAuth重定向网址保留为空”。

    仍然错过字段但启用登录成功的配置: 在模板login.html中,使用'linkedin'

      <a href="{% provider_login_url 'linkedin' %}" class="btn btn-block btn-social btn-linkedin">
    

    在settings.py中,使用INSTALLED_APPS中的“linkedin”和SOCIALACCOUNT_PROVIDERS中的“linkedin_oauth2”。

    我没有将''r_contactinfo','r_network'}放在'SCOPE'中,因为它需要'Apply with Linkedin'程序申请和批准。

    INSTALLED_APPS=(
        ...
        'allauth',
        'allauth.account',
        'allauth.socialaccount',
        #providers
        'allauth.socialaccount.providers.linkedin',
      ....
     )
    
    
    SOCIALACCOUNT_PROVIDERS = \
        {'linkedin_oauth2':
              {'SCOPE': ['r_emailaddress', 'r_basicprofile'],
               'PROFILE_FIELDS': ['id',
                                 'first-name',
                                 'last-name',
                                 'email-address',
                                 'picture-url',
                                 'picture-urls::(original)', # picture-urls::(original) is higher resolution
                                 'public-profile-url',
                                 'skills',
                                 'headline'
                                 'location',
                                 'industry',
                                 ]}
        }
    

    所有失败的配置如下。 在INSTALLED_APPS中仅使用linkedin_oauth2。仍然在SOCIALACCOUNT_PROVIDERS

    中使用linkedin_auth2
    INSTALLED_APPS=(
        ...
        'allauth',
        'allauth.account',
        'allauth.socialaccount',
        #providers
        'allauth.socialaccount.providers.linkedin_oauth2',
      ....
     )
    

    login.html可以

    OR

    在我的调试日志消息中,这是典型的错误。

    linkedin_oauth2中使用provider_login_url会导致

    ERROR 13/Dec/2015 19:22:48 base 1217 139681546774272  [django.request:256] Internal Server Error: /accounts/linkedin_oauth2/login/
    Traceback (most recent call last):
      File "/opt/python/run/venv/lib/python3.4/site-packages/django/core/handlers/base.py", line 132, in get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "/opt/python/run/venv/lib/python3.4/site-packages/allauth/socialaccount/providers/oauth2/views.py", line 55, in view
        return self.dispatch(request, *args, **kwargs)
      File "/opt/python/run/venv/lib/python3.4/site-packages/allauth/socialaccount/providers/oauth2/views.py", line 78, in dispatch
        app = provider.get_app(self.request)
      File "/opt/python/run/venv/lib/python3.4/site-packages/allauth/socialaccount/providers/base.py", line 38, in get_app
        return SocialApp.objects.get_current(self.id, request)
      File "/opt/python/run/venv/lib/python3.4/site-packages/allauth/socialaccount/models.py", line 31, in get_current
        provider=provider)
      File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method
        return getattr(self.get_queryset(), name)(*args, **kwargs)
      File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/query.py", line 334, in get
        self.model._meta.object_name
    allauth.socialaccount.models.DoesNotExist: SocialApp matching query does not exist.
    

    OR 如果INSTALLED_APPS中仅包含linkedin_oauth2,则使用linkedin中的provider_login_url会导致此

      File "/opt/python/run/venv/lib/python3.4/site-packages/django/template/defaulttags.py", line 329, in render
        return nodelist.render(context)
      File "/opt/python/run/venv/lib/python3.4/site-packages/django/template/base.py", line 903, in render
        bit = self.render_node(node, context)
      File "/opt/python/run/venv/lib/python3.4/site-packages/django/template/base.py", line 917, in render_node
        return node.render(context)
      File "/opt/python/run/venv/lib/python3.4/site-packages/allauth/socialaccount/templatetags/socialaccount.py", line 17, in render
        provider = providers.registry.by_id(provider_id)
      File "/opt/python/run/venv/lib/python3.4/site-packages/allauth/socialaccount/providers/__init__.py", line 20, in by_id
        return self.provider_map[id]
    KeyError: 'linkedin'
    

2 个答案:

答案 0 :(得分:1)

你必须使用“linkedin_oauth2”而不是“linkedin”。

href="{% provider_login_url 'linkedin_oauth2' %}" class="btn btn-block btn-social btn-linkedin"

答案 1 :(得分:0)

这对于可能遇到此问题的其他人有帮助。我通过更改在Linked OAuth2的api手册中更新的LinkedOAuth2的token_url修复了这个问题。