![](/img/trans.png)
[英]python-social-auth Facebook and LinkedIn user's email is empty
[英]Get user email in Python-social-auth
我已將其包含在我的 settings.py 文件中:
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
我如何在管道中訪問 Facebook 用戶的電子郵件。 我正在使用 pythons-social-auth
我想使用來自 facebook 的用戶電子郵件地址並檢查使用該電子郵件的用戶是否已經存在。
您必須自定義管道方法或向管道添加新方法。
例如,您可以自定義create_user
方法:
def create_user(strategy, details, user=None, *args, **kwargs):
email = details.get('email', '')
if UserClass.objects.filter(email=email).exists():
// do whatever you want
if user:
return {'is_new': False}
fields = dict((name, kwargs.get(name) or details.get(name))
for name in strategy.setting('USER_FIELDS',
USER_FIELDS))
if not fields:
return
return {
'is_new': True,
'user': strategy.create_user(**fields)
}
然后在您的設置文件中,指明此方法的路徑:
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'path.to.your.custom.create_user', // indicate the path here
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details'
)
希望這可以幫助
更新:
如果您沒有在details
參數中收到email
,請嘗試以下操作:
在管道的social_details
方法中打印response
變量:
def social_details(backend, details, response, *args, **kwargs):
print response
return {'details': dict(backend.get_user_details(response),
**details)}
如果響應包含email
請在日志中查看。 如果它確實包含email
,則意味着email
只是沒有包含在details
變量的details
參數中。
如果沒有, SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
有效? 我的意思是,Facebook 是否要求您在登錄時授予此范圍?
如果是,則授予它。 如果沒有,請嘗試從 Facebook 刪除您的應用,然后重新注冊。 它有時會發生。
這可能是一個老問題,但答案對很多人來說都很重要。 所以我們開始了。
首先授予scope
,然后harvest
數據。 所以你的 django settings.py
文件需要這兩個設置(除了key
和secret
)
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
'locale': 'en',
'fields': 'id, name, email,
}
請注意,在身份驗證期間,即使您已要求,用戶也可能決定不提供電子郵件。 因此,不要使用response['email']
獲取電子郵件,而是使用response.get('email', None)
。 然后處理不提供電子郵件的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.