def post(self):
        """Fetches a request token and redirects the user to the approval page."""

        self.session = Session(self)
        #logging.warning(self.session.cookie.output())

        if users.get_current_user():
            # 1.) REQUEST TOKEN STEP. Provide the data scope(s) and the page we'll
            # be redirected back to after the user grants access on the approval page.
            req_token = gcal.FetchOAuthRequestToken(
                scopes=AUTH_SETTINGS['SCOPES'], oauth_callback=self.request.uri)

            # When using HMAC, persist the token secret in order to re-create an
            # OAuthToken object coming back from the approval page.
            #logging.warning(req_token.secret)
            self.session['oauth_token_secret'] = req_token.secret
            self.session.save()
            #logging.warning(self.session['oauth_token_secret'])
            #logging.warning(self.session.keys())
            #logging.warning(self.session.cookie.output())

            # Generate the URL to redirect the user to.  Add the hd paramter for a
            # better user experience.  Leaving it off will give the user the choice
            # of what account (Google vs. Google Apps) to login with.
            domain = self.request.get('domain', default_value='default')
            approval_page_url = gcal.GenerateOAuthAuthorizationURL(
                extra_params={'hd': domain})

            # 2.) APPROVAL STEP.  Redirect to user to Google's OAuth approval page.
            self.redirect(approval_page_url)
    def get(self):
        """Invoked after we're redirected back from the approval page."""

        self.session = Session(self)
        #logging.warning(self.session.cookie.output())

        oauth_token = gdata.auth.OAuthTokenFromUrl(self.request.uri)
        if oauth_token:
            #logging.warning(oauth_token)
            #logging.warning(self.session.__str__())
            #logging.warning(self.session.cookie.output())
            oauth_token.secret = self.session['oauth_token_secret']
            #logging.warning(self.session['oauth_token_secret'])
            oauth_token.oauth_input_params = gcal.GetOAuthInputParameters()
            gcal.SetOAuthToken(oauth_token)

            # 3.) Exchange the authorized request token for an access token
            oauth_verifier = self.request.get('oauth_verifier', default_value='')
            access_token = gcal.UpgradeToOAuthAccessToken(
                oauth_verifier=oauth_verifier)

            # Remember the access token in the current user's token store
            if access_token and users.get_current_user():
                gcal.token_store.add_token(access_token)
            elif access_token:
                gcal.current_token = access_token
                gcal.SetOAuthToken(access_token)

        self.redirect(calendar_prefix)
class OAuthDance(webapp2.RequestHandler):
    """Handler for the 3 legged OAuth dance, v1.0a."""

    """This handler is responsible for fetching an initial OAuth request token,
      redirecting the user to the approval page.  When the user grants access, they
      will be redirected back to this GET handler and their authorized request token
      will be exchanged for a long - lived access token."""

    # GET /get_oauth_token
    def get(self):
        """Invoked after we're redirected back from the approval page."""

        self.session = Session(self)
        #logging.warning(self.session.cookie.output())

        oauth_token = gdata.auth.OAuthTokenFromUrl(self.request.uri)
        if oauth_token:
            #logging.warning(oauth_token)
            #logging.warning(self.session.__str__())
            #logging.warning(self.session.cookie.output())
            oauth_token.secret = self.session['oauth_token_secret']
            #logging.warning(self.session['oauth_token_secret'])
            oauth_token.oauth_input_params = gcal.GetOAuthInputParameters()
            gcal.SetOAuthToken(oauth_token)

            # 3.) Exchange the authorized request token for an access token
            oauth_verifier = self.request.get('oauth_verifier', default_value='')
            access_token = gcal.UpgradeToOAuthAccessToken(
                oauth_verifier=oauth_verifier)

            # Remember the access token in the current user's token store
            if access_token and users.get_current_user():
                gcal.token_store.add_token(access_token)
            elif access_token:
                gcal.current_token = access_token
                gcal.SetOAuthToken(access_token)

        self.redirect(calendar_prefix)

    # POST /get_oauth_token
    def post(self):
        """Fetches a request token and redirects the user to the approval page."""

        self.session = Session(self)
        #logging.warning(self.session.cookie.output())

        if users.get_current_user():
            # 1.) REQUEST TOKEN STEP. Provide the data scope(s) and the page we'll
            # be redirected back to after the user grants access on the approval page.
            req_token = gcal.FetchOAuthRequestToken(
                scopes=AUTH_SETTINGS['SCOPES'], oauth_callback=self.request.uri)

            # When using HMAC, persist the token secret in order to re-create an
            # OAuthToken object coming back from the approval page.
            #logging.warning(req_token.secret)
            self.session['oauth_token_secret'] = req_token.secret
            self.session.save()
            #logging.warning(self.session['oauth_token_secret'])
            #logging.warning(self.session.keys())
            #logging.warning(self.session.cookie.output())

            # Generate the URL to redirect the user to.  Add the hd paramter for a
            # better user experience.  Leaving it off will give the user the choice
            # of what account (Google vs. Google Apps) to login with.
            domain = self.request.get('domain', default_value='default')
            approval_page_url = gcal.GenerateOAuthAuthorizationURL(
                extra_params={'hd': domain})

            # 2.) APPROVAL STEP.  Redirect to user to Google's OAuth approval page.
            self.redirect(approval_page_url)