def refresh_bearer_token(self, force=False): """Refreshes the OAuth token if necessary. :param bool force: Force-refresh the bearer token. """ if not self.use_oauth: return # Skip refresh if it's not necessary and not forced if self.expires > time.time() and not force: return # Subtracting 30 seconds is arbitrary to avoid potential time discrepencies start_time = int(time.time() - 30) data = { 'client_id': _client_id, 'client_secret': _client_secret, 'grant_type': 'refresh_token', 'refresh_token': self.refresh_token } response = request._execute_request( 'https://oauth2.googleapis.com/token', 'POST', headers={'Content-Type': 'application/json'}, data=data) response_data = json.loads(response.read()) self.access_token = response_data['access_token'] self.expires = start_time + response_data['expires_in'] self.cache_tokens()
def _call_api(self, endpoint, query, data): """Make a request to a given endpoint with the provided query parameters and data.""" endpoint_url = f'{endpoint}?{parse.urlencode(query)}' response = request._execute_request(endpoint_url, 'POST', headers={ 'Content-Type': 'application/json', }, data=data) return json.loads(response.read())
def fetch_bearer_token(self): """Fetch an OAuth token.""" # Subtracting 30 seconds is arbitrary to avoid potential time discrepencies start_time = int(time.time() - 30) data = { 'client_id': _client_id, 'scope': 'https://www.googleapis.com/auth/youtube' } response = request._execute_request( 'https://oauth2.googleapis.com/device/code', 'POST', headers={'Content-Type': 'application/json'}, data=data) response_data = json.loads(response.read()) verification_url = response_data['verification_url'] user_code = response_data['user_code'] print(f'Please open {verification_url} and input code {user_code}') input('Press enter when you have completed this step.') data = { 'client_id': _client_id, 'client_secret': _client_secret, 'device_code': response_data['device_code'], 'grant_type': 'urn:ietf:params:oauth:grant-type:device_code' } response = request._execute_request( 'https://oauth2.googleapis.com/token', 'POST', headers={'Content-Type': 'application/json'}, data=data) response_data = json.loads(response.read()) self.access_token = response_data['access_token'] self.refresh_token = response_data['refresh_token'] self.expires = start_time + response_data['expires_in'] self.cache_tokens()
def _call_api(self, endpoint, query, data): """Make a request to a given endpoint with the provided query parameters and data.""" endpoint_url = f'{endpoint}?{parse.urlencode(query)}' headers = { 'Content-Type': 'application/json', } # Add the bearer token if applicable if self.bearer_token: self.refresh_bearer_token() headers['authorization'] = f'Bearer {self.bearer_token}' response = request._execute_request(endpoint_url, 'POST', headers=headers, data=data) return json.loads(response.read())
def _call_api(self, endpoint, query, data): """Make a request to a given endpoint with the provided query parameters and data.""" # Remove the API key if oauth is being used. if self.use_oauth: del query['key'] endpoint_url = f'{endpoint}?{parse.urlencode(query)}' headers = { 'Content-Type': 'application/json', } # Add the bearer token if applicable if self.use_oauth: if self.access_token: self.refresh_bearer_token() headers['Authorization'] = f'Bearer {self.access_token}' else: self.fetch_bearer_token() headers['Authorization'] = f'Bearer {self.access_token}' response = request._execute_request(endpoint_url, 'POST', headers=headers, data=data) return json.loads(response.read())