无法设置和检索会话变量

时间:2017-03-01 23:20:01

标签: python-2.7 django-views session-cookies django-1.10

使用Django 1.10

我的中间件设置如下:

MIDDLEWARE = [
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',

'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',


'wagtail.wagtailcore.middleware.SiteMiddleware',
'wagtail.wagtailredirects.middleware.RedirectMiddleware',
]

我确保包含SESSION ENGINE

SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'

我的客户端密码和ID设置如下:

os.environ['CLIENT_SECRET'] = 'something'
os.environ['CLIENT_ID'] = 'something'

我的视图设置为执行回调,存储会话变量如下:

def potato_oauth_callback(request):


  POTATO_OAUTH2_TOKEN_ENDPOINT = "https://login.potato.com/potatoservices/potatooauth2/potatotoken"
  POTATO_SECRET = get from env variables
  POTATO_ID = get from env variables
  POTATO_TYPE = "authorization_code"
  POTATOCALLBACK_URL = "https://potatourl/potatoes/potatocallback"

  authorization_code = request.GET.get('code')

  payload = {
    'potato_type': POTATO_TYPE,
    'potato_id': POTATO_ID,
    'potato_secret': POTATO_SECRET,
    'potato_uri': POTATOCALLBACK_URL,
    'code': potato_code
  }

  r = requests.post(POTATO_OAUTH2_TOKEN_ENDPOINT, data=potatoload)

  resp = r.json()

  potatoAccess = {
    'id': resp['id'],
    'timestamp': resp['issued_at'],
    'instanceURL': resp['instance_url'],
    'signature': resp['signature'],
    'accessToken': resp['access_token']
  }

  request.session['potatoAccess_id'] = potatoAccess['id']
  request.session['potatoAccess_timestamp'] = potatoAccess['timestamp']
  request.session['potatoAccess_instanceURL'] = potatoAccess['instanceURL']
  request.session['potatoAccess_signature'] = potatoAccess['signature']
  request.session['potatoAccess_accessToken'] = potatoAccess['accessToken']

  return HttpResponse(t.render({}, request))

随后,当我发出重新检查会话变量的请求时,我一直收到错误并且无法检索它们:

def check_potatoes(request):
  c = {}
  c['potatoAccess_id'] = request.session['potatoAccess_id']
  print request.session['potatoAccess_id']
  return HttpResponse(json.dumps('potatoes'), content_type='application/json')

OR

def check_potatoes(request):
  try:
    c = {}
    c['potatoAccess_id'] = request.session['potatoAccess_id']
    print request.session['potatoAccess_id']
    return HttpResponse(json.dumps('potatoes'), content_type='application/json')
  except Exception as potatoes:
    potatoesdata = {'message': potatoes.message}
    return HttpResponse(json.dumps(potatoesdata), content_type='application/json')

需要注意的是,当我进行初始回调时,我可以验证:

request.session['potatoAccess_id'] returns a value
potatoAccess['id'] returns a value
更改视图时的

(通过ajax调用的其他方法) 我得到这些关键错误,好像价值从未存在

1 个答案:

答案 0 :(得分:0)

初学者错误 - 回调是使用隧道服务ngrok.io发送的,对于那些你不熟悉它的人来说(所以我实际上可以有一个安全的连接),其余的会话我只是在我的localhost上做,所以我的设置是是的,我只是不在同一个会话中,因此为什么我的变量不存在......

我的登录'正在https://localhost:8000/login完成, 但是,回调是在ngrok上进行的 " https://login.potato.com/potatoservices/potatooauth2/potatotoken"

当会话变量可以从https://xxx.ngrok.io/potatourls

获得时,我继续测试我的localhost