Alexey Zinger
Alexey Zinger

Reputation: 1

error serializaing GAE session with webapp2_extras

I am not new to App Engine, but am just getting started with the Python stack on it. I am using webapp2 and Cygwin with its own Python. I am trying to implement custom authentication, relying on sessions.. I am following numerous examples of enabling sessions using webapp2-extras module. I have the following relevant code:

import webapp2

from webapp2_extras import sessions

class BaseHandler(webapp2.RequestHandler):
    def dispatch(self):
        self.session_store = sessions.get_store(request=self.request)
        try:
            webapp2.RequestHandler.dispatch(self)
        finally:
            self.session_store.save_sessions(self.response)

    @webapp2.cached_property
    def session(self):
        return self.session_store.get_session(self.response, backend='datastore')

class CounterHandler(BaseHandler):
    def get(self):
        cnt_key = 'cnt'
        if cnt_key in self.session:
            cnt = int(self.session[cnt_key])
        else:
            cnt = 0
        cnt += 1
        self.session[cnt_key] = str(cnt)

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write("Hello, world!")

This fails with the following stack trace:

Traceback (most recent call last):
  File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/cygdrive/c/Documents and Settings/dev/My Documents/proj/easytime_gae/src/handlers.py", line 11, in dispatch
    self.session_store.save_sessions(self.response)
  File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2_extras/sessions.py", line 420, in save_sessions
    session.save_session(response)
  File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2_extras/appengine/sessions_ndb.py", line 117, in save_session
    response, self.name, {'_sid': self.sid}, **self.session_args)
  File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2_extras/sessions.py", line 423, in save_secure_cookie
    value = self.serializer.serialize(name, value)
  File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2_extras/securecookie.py", line 48, in serialize
    signature = self._get_signature(name, value, timestamp)
  File "/cygdrive/c/google_appengine/lib/webapp2-2.5.2/webapp2_extras/securecookie.py", line 103, in _get_signature
    signature.update('|'.join(parts))
TypeError: sequence item 0: expected string, Response found

The error goes away if I disable the line that writes to the session object and leave reading from sessions intact:

        # self.session[cnt_key] = str(cnt)

Any help would be much appreciated.

Upvotes: 0

Views: 46

Answers (1)

Greg
Greg

Reputation: 10360

The first argument to get_session is the name, if you want to change from the default. You are passing a response object, which is wrong.

@webapp2.cached_property
def session(self):
    return self.session_store.get_session(backend='datastore')

Upvotes: 1

Related Questions