def add_auth(self, req, **kwargs): """ add authorize information for request """ req.params['app_id'] = self.app_id if self.access_token: req.params['access_token'] = self.access_token req.params['signature_version'] = self.SignatureVersion req.params['version'] = self.APIVersion time_stamp = get_ts() req.params['time_stamp'] = time_stamp qs, signature = self._calc_signature(req.params, req.method, req.auth_path) # print 'query_string: %s Signature: %s' % (qs, signature) if req.method == 'POST': # req and retried req should not have signature params = req.params.copy() params["signature"] = signature req.body = urllib.urlencode(params) req.header = { 'Content-Length': str(len(req.body)), 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain', 'Connection': 'Keep-Alive' } else: req.body = '' # if this is a retried req, the qs from the previous try will # already be there, we need to get rid of that and rebuild it req.path = req.path.split('?')[0] req.path = (req.path + '?' + qs + '&signature=' + signature)
def test_get_ts(self): ts = 1391832000 ts = time.localtime(ts) expected = '2014-02-08T12:00:00Z' self.assertEqual(get_ts(ts), expected) self.assertTrue(isinstance(get_ts(), str))