问题描述
我正在使用Django social_auth API通过社交账户进行登录。在这里,我想从社交帐户获取电子邮件地址并将其存储在我的数据库表中。可以从帐户中检索名字和姓氏,但无法检索电子邮件地址,个人资料图片。请分享您从社交帐户中获取这些详细信息的想法。
I'm using Django social_auth api for login via social account. Here I want to get email address from social account and stored it in my database table. The first-name and last-name can be retrieved from the account but I can't retrieved email address, profile picture. Please share your ideas for retrieving those details from social account.
推荐答案
Twitter不会透露电子邮件,以检索您需要的Facebook电子邮件定义 FACEBOOK_EXTENDED_PERMISSIONS = ['email']
,LinkedIn电子邮件将自动检索。电子邮件存储在电子邮件
属性下的用户模型中。
Twitter doesn't disclose emails, to retrieve Facebook email you need to define FACEBOOK_EXTENDED_PERMISSIONS = ['email']
, LinkedIn email is retrieved automatically. Emails are stored in the user model under the email
attribute.
可以通过定义此设置来存储个人资料图片:
Profile picture can be stored by defining this settings:
TWITTER_EXTRA_DATA = [('profile_image_url', 'profile_picture')]
LINKEDIN_EXTRA_DATA = [('picture-url', 'profile_picture')]
Facebook个人资料图片可通过API访问,而不是在验证过程中发送。你可以定义你的管道来存储它,如下所示:
Facebook profile picture is accessible by API and not sent during the auth processes. You can define your pipeline to store it like this:
from django.utils import simplejson
from social_auth.utils import dsa_urlopen
def facebook_profile_picture(backend, user, social_user, details, response, *args, **kwargs):
if backend.name != 'facebook':
return
url = 'https://graph.facebook.com/{0}/picture?redirect=false&access_token={1}'
response = dsa_urlopen(url.format(social_user.extra_data['id'], social_user.extra_data['access_token'])
data = simplejson.load(response)
social_user.extra_data['profile_picture'] = data['data']['url']
social_user.save()
将其添加到管道底部的设置(检查管道文档请访问)。这段代码没有测试,所以玩了一点
Add it to the bottom of the pipelines setting (check the pipelines doc at http://django-social-auth.readthedocs.org/en/latest/pipeline.html). This code wasn't tested, so play a little with it.
然后,您可以访问个人资料图片:
Then you can access the profile picture doing:
social = user.social_auth.get(provider='facebook') # or twitter or linkedin
social.extra_data['profile_picture']
这篇关于Django Social Auth:从linkedin,twitter& Facebook的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!