def get_token(self):
<EOL>now = datetime.datetime.utcnow()<EOL>_not_before = now - datetime.timedelta(minutes=TOKEN_SKEW)<EOL>not_before = _not_before.strftime(TIME_FORMAT)<EOL>_not_after = now + datetime.timedelta(<EOL>minutes=self.token_lifetime - TOKEN_SKEW<EOL>)<EOL>not_after = _not_after.strftime(TIME_FORMAT)<EOL>payload = json.dumps({<EOL>'<STR_LIT>': not_before,<EOL>'<STR_LIT>': not_after<EOL>})<EOL>token = self._get_cached_token()<EOL>if token:<EOL><INDENT>return token<EOL><DEDENT>try:<EOL><INDENT>token = self.kms_client.encrypt(<EOL>KeyId=self.auth_key,<EOL>Plaintext=payload,<EOL>EncryptionContext=self.auth_context<EOL>)['<STR_LIT>']<EOL>token = base64.b64encode(ensure_bytes(token))<EOL><DEDENT>except (ConnectionError, EndpointConnectionError) as e:<EOL><INDENT>logging.exception('<STR_LIT>'.format(str(e)))<EOL>raise ServiceConnectionError()<EOL><DEDENT>except Exception:<EOL><INDENT>logging.exception('<STR_LIT>')<EOL>raise TokenGenerationError()<EOL><DEDENT>self._cache_token(token, not_after)<EOL>return token<EOL>
Get an authentication token.