def _refresh_token(self): """refresh access token""" headers = dict() headers[ 'Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8' params = dict() params['grant_type'] = 'client_credentials' params['client_secret'] = self.app_secret params['client_id'] = self.app_id msg_body = urllib.urlencode(params) try: response = _http.post(self.token_server, msg_body, headers) if response.status_code is not 200: return False, 'http status code is {0} in get access token'.format( response.status_code) """ json string to directory """ response_body = json.loads(response.text) self.access_token = response_body.get('access_token') self.token_expired_time = long(round(time.time() * 1000)) + ( long(response_body.get('expires_in')) - 5 * 60) * 1000 return True, None except Exception as e: raise ApiCallError(format(repr(e)))
def _send_to_server(cls, headers, body, url, verify_peer=False): try: msg_body = json.dumps(body) response = _http.post(url, msg_body, headers, verify_peer) if response.status_code is not 200: raise ApiCallError('http status code is {0} in send.'.format( response.status_code)) # json text to dict resp_dict = json.loads(response.text) return resp_dict except Exception as e: raise ApiCallError('caught exception when send. {0}'.format(e))
def _refresh_token(self, verify_peer=False): """refresh access token :param verify_peer: (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to ``True``. """ headers = dict() headers[ 'Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8' params = dict() params['grant_type'] = 'client_credentials' params['client_secret'] = self.app_secret_at params['client_id'] = self.app_id_at msg_body = urllib.parse.urlencode(params) try: response = _http.post(self.token_server, msg_body, headers, verify_peer=verify_peer) if response.status_code is not 200: return False, 'http status code is {0} in get access token'.format( response.status_code) """ json string to directory """ response_body = json.loads(response.text) self.access_token = response_body.get('access_token') self.token_expired_time = int(round( time.time() * 1000)) + (int(response_body.get('expires_in')) - 5 * 60) * 1000 return True, None except Exception as e: raise ApiCallError(format(repr(e)))