def urlread(self, url): """Wraps util.urlread() and passes through the access token. """ if self.access_token: parsed = list(urlparse.urlparse(url)) # query params are in index 4 # TODO: when this is on python 2.7, switch to urlparse.parse_qsl params = cgi.parse_qsl(parsed[4]) + [("access_token", self.access_token)] parsed[4] = urllib.urlencode(params) url = urlparse.urlunparse(parsed) return util.urlread(url)
def urlread(self, url, app_key=None, app_secret=None, **kwargs): """Wraps util.urlread() and adds an OAuth signature. TODO: unit test this """ auth = tweepy.OAuthHandler(appengine_config.TWITTER_APP_KEY, appengine_config.TWITTER_APP_SECRET) # make sure token key and secret aren't unicode because python's hmac # module (used by tweepy/oauth.py) expects strings. # http://stackoverflow.com/questions/11396789 auth.set_access_token(str(self.access_token_key), str(self.access_token_secret)) method = kwargs.get('method', 'GET') parsed = urlparse.urlparse(url) url_without_query = urlparse.urlunparse(list(parsed[0:4]) + ['', '']) headers = kwargs.setdefault('headers', {}) auth.apply_auth(url_without_query, method, headers, # TODO: switch to urlparse.parse_qsl after python27 runtime dict(cgi.parse_qsl(parsed.query))) logging.info('Populated Authorization header from access token: %s', headers.get('Authorization')) return util.urlread(url, **kwargs)