【问题标题】:Using Python Social-auth how to retrieve fields stored in session from custom pipeline使用 Python Social-auth 如何从自定义管道中检索存储在会话中的字段
【发布时间】:2014-09-10 16:42:11
【问题描述】:

我的应用程序使用 python social-auth 进行登录以及允许帐户“连接”。考虑到这一点,我有一个自定义管道如下:

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',
'jdconnections.utils.get_username', #<<here
'social.pipeline.social_auth.associate_by_email', 
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details'
)

我也设置了:

SOCIAL_AUTH_FIELDS_STORED_IN_SESSION = ['connection',] 

在我的应用视图中,我将 social-auth 称为如下,例如 twitter:

  return HttpResponseRedirect(reverse('social:begin', args=('twitter',)) + "?connection=" + request.POST.get('origin') )

这工作正常,我前往 twitter 并完成了身份验证,但是当我回到我的自定义管道工具时,我无法检索会话值,这就是我正在做的事情:

from django.conf import settings
from django.contrib.sessions.backends.db import SessionStore
from jdconnections.models import Connections

from social.pipeline.user import get_username as social_get_username

def get_username(strategy, details, user=None, *args, **kwargs):

   current_session = SessionStore()
   if 'connection' not in current_session:
      result = social_get_username(strategy, details, user=user, *args, **kwargs)   
      return result
   else:
    if current_session['connection'] == 'TW':
        social = user.social_auth.get(provider='twitter')
        access_token = social.extra_data['access_token']
        cn = Connections.objects.create(origin='TW',ctoken = access_token)
    return None

使用 PDB 和 django 调试工具栏我可以看到值在那里,这段代码有什么问题,它没有检索到它?感谢您的帮助!

【问题讨论】:

    标签: python django python-social-auth


    【解决方案1】:

    根据文档,检索数据的正确方法是:

    strategy.session_get('connection')
    

    也许这有帮助?

    http://psa.matiasaguirre.net/docs/use_cases.html#pass-custom-get-post-parameters-and-retrieve-them-on-authentication

    【讨论】:

      猜你喜欢
      • 2014-10-01
      • 2015-02-24
      • 1970-01-01
      • 2012-10-26
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多