def register_by_access_token(request, backend):
# This view expects an access_token GET parameter, if it's needed,
# request.backend and request.strategy will be loaded with the current
# backend and strategy.
token = request.GET.get('access_token')
user = backend.do_auth(request.GET.get('access_token'))
if user:
login(request, user)
return 'OK'
else:
return 'ERROR'
此示例来自不同的资源。我使用django 1.9,但是这个代码失败,'GET'不存在,我重写了这个函数:
def UserLogin(request, backend, details, response, *args, **kwargs):
# This view expects an access_token GET parameter, if it's needed,
# request.backend and request.strategy will be loaded with the current
# backend and strategy.
token = response['access_token']
user = backend.do_auth(token)
arg = {}
arg['user'] = user
if user:
login(request, user)
return HttpResponseRedirect('/', arg)
因此,我有重定向循环和服务器返回http错误,重定向过多。
毕竟我尝试编写自己的后端:
class AuthBackend(object):
def authenticate(self, access_token, id, add_info, **kwargs):
print 'authenticate backend'
user = None
print 'access_token='+access_token
try:
prof = qProfile.objects.get(facebook_uid=id)
user = User.objects.get(pk=prof.muser_id)
print "i've found this user and return pk"
except Exception as Error:
try:
print 'kwargs:'+str(kwargs)
print "can't get user and have this error:"+str(Error)
user = User.objects.create(email = add_info['email'])
user.first_name = add_info['first_name']
user.last_name = add_info['last_name']
user.username = add_info['username']
user.save()
new_profile = qProfile.objects.create(facebook_uid=id, muser_id=user.pk)
new_profile.token = access_token
new_profile.save()
except Exception as Error2:
print 'new profile error:'+str(Error2)
finally:
return user
print 'user:'+str(user)
def get_user(self, user_id):
try:
prof = qProfile.objects.get(facebook_uid=id)
user = User.objects.get(pk=prof.muser_id)
return user
except User.DoesNotExist:
return None
这是观点:
@csrf_exempt
def UserLogin(request, backend, details, response, *args, **kwargs):
arg = {}
arg['user'] = authenticate(access_token = response['access_token'], add_info = details, id = response['id'])
print "user:='"+str(arg['user'])+"'"
request['user'] = arg['user']
try:
login(request, arg['user'])
except Exception as Error:
print 'auth failed, error:'+ str(Error)
finally:
return HttpResponseRedirect('/', arg)
Resault:'QueryDict'对象没有属性'session'
UserLogin我已经在setting.py
中添加到管道的末尾现在我有了这个:
@csrf_exempt
def UserLogin(request, backend, details, response, *args, **kwargs):
arg = {}
arg['user'] = authenticate(access_token = response['access_token'], add_info = details, id = response['id'])
request.session = SessionStore()
request.session.time = response['access_token']
request.session.save()
raise sdfds
print "user:='"+str(arg['user'])+"'"
request['user'] = arg['user']
try:
login(request, arg['user'])
except Exception as Error:
print 'auth failed, error:'+ str(Error)
finally:
return HttpResponseRedirect('/', arg)
但是现在:'QueryDict'对象没有属性'META' 几天我不能设置这个......也许我做错了什么?
解决方案:我们必须使用strategy.request来访问django请求
答案 0 :(得分:1)
对于其他迷惑于此问题的人,请参见github:https://docs.microsoft.com/en-us/iis/configuration/system.webserver/applicationinitialization/上的此问题。使用strategy.request
使请求对象可用。 strategy
将自动传递到您的管道。
像这样使用它:
def my_pipeline(strategy, *args, **kwargs):
request = strategy.request
# something...