Add authentication check to validate provided credentials

This commit is contained in:
sigma67 2020-03-27 12:35:00 +01:00
commit 62e1decf1c
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0

View file

@ -29,12 +29,19 @@ class YTMusic:
self.auth = auth
file = auth if auth else pkg_resources.resource_filename('ytmusicapi', 'headers.json')
with open(file) as json_file:
self.headers = json.load(json_file)
with open(pkg_resources.resource_filename('ytmusicapi', 'context.json')) as json_file:
self.context = json.load(json_file)
# verify authentication credentials work
if auth:
response = self.__send_request('guide', {})
if 'error' in response:
raise Exception("The provided credentials are invalid. Reason given by the server: " + response['error']['status'])
def __send_request(self, endpoint, body, additionalParams=""):
body.update(self.context)
response = requests.post(base_url + endpoint + params + additionalParams, json=body, headers=self.headers)