获取个人资料Linkedin个人资料图片django allauth

获取个人资料Linkedin个人资料图片django allauth,django,django-allauth,Django,Django Allauth,我知道这应该是一个问题,但在头痛之后,我想我应该帮助一些人解决同样的问题 我使用的是Allauth 0.42.0和Django 3.0.8,遵循Allauth文档,我无法从用户那里获取个人资料图片url。但数小时的搜索让我找到了这个解决方案: # access_token required by LinkedIn's API access_token = SocialToken.objects.get(account__user=user, account__provider='linkedin

我知道这应该是一个问题,但在头痛之后,我想我应该帮助一些人解决同样的问题


我使用的是Allauth 0.42.0和Django 3.0.8,遵循Allauth文档,我无法从用户那里获取个人资料图片url。

但数小时的搜索让我找到了这个解决方案:

# access_token required by LinkedIn's API
access_token = SocialToken.objects.get(account__user=user, account__provider='linkedin_oauth2')

# The request
r = requests.get(f"https://api.linkedin.com/v2/me?projection=(profilePicture("
                         f"displayImage~:playableStreams))&oauth2_access_token={access_token}")

# The json on the profile picture key
profile_pic_json = r.json().get('profilePicture')

# There's a lot of pictures sizes, so I put it on a array to easier reading
elements = profile_pic_json['displayImage~']['elements']

# elements[len(elements)-1] returns the pic with the highest resolution
# ['identifiers'][0]['identifier'] is the url itself
url_picture = elements[len(elements)-1]['identifiers'][0]['identifier']
我希望这能帮助别人