'RequestHandler for the OAuth 2.0 redirect callback.
Usage:
app = webapp.WSGIApplication([
(\'/index\', MyIndexHandler),
(decorator.callback_path, decorator.callback_handler())
Returns:
A webapp.RequestHandler that handles the redirect back from the
server during the OAuth 2.0 dance.'

def callback_handler(self):

    decorator = self
    class OAuth2Handler(webapp.RequestHandler, ):
        'Handler    for    the    redirect_uri    of    the    OAuth    2.0    dance.'
        @login_required
        def get(self):
            error = self.request.get('error')
            if error:
                errormsg = self.request.get('error_description', error)
                self.response.out.write(('The    authorization    request    failed:    %s' % errormsg))
            else:
                user = users.get_current_user()
                decorator._create_flow(self)
                credentials = decorator.flow.step2_exchange(self.request.params)
                StorageByKeyName(CredentialsModel, user.user_id(), 'credentials').put(credentials)
                self.redirect(str(self.request.get('state')))
    return OAuth2Handler
