我正在使用的应用程序覆盖了Python Social Auth /complete/<backend>/
端点的端点。
在urls.py
:
urlspatterns = [
...
# Override of social_auth
url(r'^api/v1/auth/oauth/complete/(?P<backend>[^/]+)/$',
social_auth_complete,
name='social_complete'),
...
]
在views.py
内:
from social_django.views import complete
def social_auth_complete(request, backend, *args, **kwargs):
"""Overwritten social_auth_complete."""
# some custom logic getting variables from session (Unrelated).
response = complete(request, backend, *args, **kwargs)
# Some custom logic adding args to the redirect (Unrelated).
我们正在尝试实施partial pipeline method。第一次调用端点时,一切都按预期工作。
@partial
def required_info(strategy, details, user=None, is_new=False, *args, **kwargs):
"""Verify the user has all the required information before proceeding."""
if not is_new:
return
for field in settings.SOCIAL_USER_REQUIRED_DATA:
if not details.get(field):
data = strategy.request_data().get(field)
if not data:
current_partial = kwargs.get('current_partial')
social_provider = kwargs.get('backend')
return strategy.redirect(f'.../?partial_token={partial_token}&provider={social_provider}'
else:
details[field] = data
这会将用户重定向到前端,在前端填写一个表单,该表单会将 POST 请求调用到原始API api/v1/auth/oauth/complete/(?P<backend>[^/]+)/
,并在数据中添加以下内容:
{
'required_fieldX':'数据',
...
'partial_token':'',
}
有两件事出错了;当我pdb进入required_info
时,strategy.request_data()
中永远不会有任何数据。 kwargs['request'].body
中仍有数据,我可以将数据带到那里。
但我担心第二次我们永远不会从社交核心进入block of code:
partial = partial_pipeline_data(backend, user, *args, **kwargs)
if partial:
user = backend.continue_pipeline(partial)
# clean partial data after usage
backend.strategy.clean_partial_pipeline(partial.token)
else:
user = backend.complete(user=user, *args, **kwargs)
我知道这是真的,因为当我询问数据库时,原始的 Partial 对象仍然存在,就像从未调用过backend.strategy.clean_partial_pipeline(partial.token)
一样。
为什么social_django.views.complete
没有按预期处理 POST 请求,因为它似乎在所有示例应用程序中。我们覆盖它有问题吗?我是否应该创建一个单独的端点来处理 POST 请求?如果是这样,我如何模仿@psa
中的所有内容,以便我可以调用backend.continue_pipeline(partial)
?
答案 0 :(得分:0)
我认为这里只有一个问题,那就是Django策略在加载请求数据时没有调查request.body
,你可以看到负责的方法here。在那里,您可以看到它会查找request.GET
和/或request.POST
,但不会查找正文。
您可以通过定义从内置策略扩展的自定义策略轻松解决此问题,并覆盖request_data
方法以查找request.body
中的值。然后定义SOCIAL_AUTH_STRATEGY
以指向您的班级。