【问题标题】:Django allauth LinkedIn API not returning full profile dataDjango allauth LinkedIn API 未返回完整的个人资料数据
【发布时间】:2019-04-03 05:30:11
【问题描述】:

我有一个正在试验 Django Allauth 的应用程序,特别是 LinkedIn api。

我在我的设置中定义范围,如下所示:

SOCIALACCOUNT_PROVIDERS = \
{
    'linkedin':
    {
        'SCOPE': ['r_fullprofile', 'r_emailaddress']
    }
}

我正在使用以下方法将此信息输出到模板:

{% extends 'base.html' %}
{% load auth_extras %}
{% load account %}
{% load socialaccount %}

{% block content %}
{% if user.is_authenticated %}

{% for key, value in user.socialaccount_set.all.0.extra_data.items %}
<ul>
    <li>{{ key }}: {{ value }}</li>
</ul>
{% endfor %}
{% endif %}
{% endblock content %}

当我通过 LinkedIn 授权一个帐户时,我可以看到它要求批准基本个人资料、电子邮件地址和完整个人资料。但是user.socialaccount_set.all.0.extra_data 对象只有基本的个人资料和电子邮件地址数据。完整的个人资料数据发生了什么变化?

此外,user.socialaccount_set.all.0.extra_data 真的是访问提供者公开的所有数据的最佳方式吗?

【问题讨论】:

    标签: django authentication oauth linkedin django-allauth


    【解决方案1】:

    我最近遇到了这个问题并遇到了这个问题:https://github.com/pennersr/django-allauth/issues/397

    简短回答:似乎如果我们在 settings.py 中定义额外的 PROFILE_FIELDS,这些字段将被复制到 extra_data。

    为了完整起见,这里是我的 settings.py(包括 facebook 和linkedin)

    SOCIALACCOUNT_PROVIDERS = \
        {'facebook': {'SCOPE': ['email', 'user_about_me', 'user_birthday',
                                'user_education_history','user_work_history',
                                'user_hometown',
                                'user_location',
                                'user_religion_politics','user_subscriptions',
                                'read_stream',
                                'read_insights',
                                'read_friendlists',
                                'user_likes',
                                'user_interests',
                                'user_groups'
                                ],
                      'AUTH_PARAMS': {},
                      'METHOD': 'oauth2'
                    },
    
         'linkedin': {'SCOPE': ['r_emailaddress', 'r_fullprofile', 'r_emailaddress', 'r_contactinfo', 'r_network'],
                      'PROFILE_FIELDS': ['id', 'first-name',
                                  'last-name',
                                  'email-address',
                                  'picture-url',
                                  'public-profile-url', 'skills', 'headline'
                      ]
         }
        }
    

    我可以使用以下方法打印技能和标题:

    @receiver(user_logged_in)
    def populate_profile_login2(request, **kwargs):
    {
        try:
            extra_data = kwargs.get('user').socialaccount_set.filter(provider='linkedin')[0].extra_data
            for key, value in extra_data.iteritems():
                print key, value
        except:
            print ' NOT LINKEDIN'
    
    
     }
    

    【讨论】:

      【解决方案2】:

      我正在使用新的linkedin_oauth2 并遇到了同样的问题。 我花了一点时间才弄清楚我的错误,我使用了错误的提供商名称('linkedin' 而不是 'linkedin_oauth2')

      希望这将在未来对其他人有所帮助。

      此 (settings.py) 配置按预期工作。

      SOCIALACCOUNT_PROVIDERS = \
              'linkedin_oauth2': {'SCOPE': ['r_fullprofile', 'r_emailaddress'],
                        'PROFILE_FIELDS': ['id', 'first-name',
                                    'last-name',
                                    'email-address',
                                    'picture-url',
                                    'public-profile-url',
                                    'skills', 'headline']}
              }
      

      【讨论】:

        【解决方案3】:

        在我的linkedin配置(settings.py)中,它适用于:

        SOCIALACCOUNT_PROVIDERS = \
        {
        'linkedin': 
            {'SCOPE': [ 'r_emailaddress',
                        'r_fullprofile',
                        'r_emailaddress',
                        'r_contactinfo',
                        'r_network'],
              'PROFILE_FIELDS':
                    [
                        'id',
                        'first-name',
                        'last-name',
        
                        'email-address',
                        'picture-url',
                        'public-profile-url',
                        'skills',
        
                        'headline',
                        'industry',
        
                        'num-connections',
                        'positions',
                        'interests',
                        'languages',
                        'certifications',
                        'educations',
                        'courses',
                        'three-current-positions',
                        'three-past-positions',
                        'recommendations-received',
                        'honors-awards'
                    ]
            }
        }
        

        所有字段都定义在: http://developer.linkedin.com/documents/profile-fields

        您必须确保您在“r_fullprofile”范围内才能获取上述所有数据。

        我正在与: django-allauth==0.14.2 和 Django==1.5.1

        希望对你有帮助!

        【讨论】:

          猜你喜欢
          • 2019-07-13
          • 1970-01-01
          • 1970-01-01
          • 2011-05-08
          • 1970-01-01
          • 2017-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多