def request_url_with_state(self): """Returns the current request URL, with the state query param if provided. """ state = self.request.get('state') if state: return util.add_query_params(self.request.path_url, [('state', state)]) else: return self.request.path_url
def urlread(self, url, **kwargs): """Wraps urllib2.urlopen() and passes through the access token. Keyword args are passed through to urllib2.Request. """ if self.access_token: url = util.add_query_params(url, [('access_token', self.access_token)]) logging.info('Fetching %s with %s', url, kwargs) return urllib2.urlopen(urllib2.Request(url, **kwargs)).read()
def to_url(self, state=None): """Returns a fully qualified callback URL based on to_path. Includes scheme, host, and optional state. """ url = self.request.host_url + self.to_path if state: # unquote first or state will be double-quoted state = urllib.unquote_plus(state) url = util.add_query_params(url, [('state', state)]) return url
def urlopen_access_token(url, access_token, api_key=None, **kwargs): """Wraps urllib2.urlopen() and adds an access_token query parameter. Kwargs are passed through to urlopen(). """ log_params = [("access_token", access_token[:4] + "...")] real_params = [("access_token", access_token)] if api_key: log_params.append(("api_key", api_key[:4] + "...")) real_params.append(("api_key", api_key)) log_url = util.add_query_params(url, log_params) logging.info("Fetching %s", log_url) url = util.add_query_params(url, real_params) if "timeout" not in kwargs: kwargs["timeout"] = appengine_config.HTTP_TIMEOUT try: return urllib2.urlopen(url, **kwargs) except BaseException, e: handlers.interpret_http_exception(e) raise
def urlopen_access_token(url, access_token, api_key=None, **kwargs): """Wraps urllib2.urlopen() and adds an access_token query parameter. Kwargs are passed through to urlopen(). """ params = [('access_token', access_token)] if api_key: params.append(('api_key', api_key)) url = util.add_query_params(url, params) try: return util.urlopen(url, **kwargs) except BaseException, e: util.interpret_http_exception(e) raise
def finish(self, auth_entity, state=None): """Called when the OAuth flow is complete. Clients may override. Args: auth_entity: a site-specific subclass of models.BaseAuth, or None if the user declined the site's OAuth authorization request. state: the string passed to StartHandler.redirect_url() """ assert self.to_path, 'No `to` URL. Did you forget to use the to() class method in your request handler mapping?' if auth_entity is None: params = [('declined', True)] else: params = [('auth_entity', auth_entity.key.urlsafe()), ('state', state)] token = auth_entity.access_token() if isinstance(token, basestring): params.append(('access_token', token)) else: params += [('access_token_key', token[0]), ('access_token_secret', token[1])] url = util.add_query_params(self.to_path, params) logging.info('Finishing OAuth flow: redirecting to %s', url) self.redirect(url)