From: Giorgio Ravera Date: Fri, 30 Oct 2020 14:42:25 +0000 (+0100) Subject: Separated URLs for oauth2 X-Git-Tag: v0.3~16 X-Git-Url: http://git.giorgioravera.it/?a=commitdiff_plain;h=bd82c8dae2e18798ad978a1d3b202f4cc73b651a;p=mercedes_me_api.git Separated URLs for oauth2 --- diff --git a/config.py b/config.py index ba3c6d7..e9b167a 100644 --- a/config.py +++ b/config.py @@ -34,8 +34,6 @@ class MercedesMeConfig: access_token = "" refresh_token = "" token_expires_in = "" - oauth_url = URL_OAUTH - res_url_prefix = URL_RES_PREFIX ######################## # Init @@ -123,6 +121,9 @@ class MercedesMeConfig: # Check Token ######################## def CheckToken(self, token): + if token == None: + _LOGGER.error ("Error reading token.") + return False if "error" in token: if "error_description" in token: _LOGGER.error ("Error retriving token: " + token["error_description"]) @@ -144,8 +145,16 @@ class MercedesMeConfig: # Create Token ######################## def CreateToken(self): + + auth_url = ( + f"{URL_OAUTH_AUTH}&" + + f"client_id={self.client_id}&" + + f"redirect_uri={REDIRECT_URL}&" + + f"scope={SCOPE}" + ) + print( "Open the browser and insert this link:\n" ) - print( "https://id.mercedes-benz.com/as/authorization.oauth2?response_type=code&client_id=" + self.client_id + "&redirect_uri=" + REDIRECT_URL + "&scope=" + SCOPE + "\n") + print(auth_url + "\n") print( "Copy the code in the url:") auth_code = input() diff --git a/const.py b/const.py index 4e71886..e6136e0 100644 --- a/const.py +++ b/const.py @@ -15,7 +15,9 @@ CREDENTIAL_FILE = ".mercedesme_credentials" RESOURCES_FILE = ".mercedesme_resources" REDIRECT_URL = "https://localhost" SCOPE = "mb:vehicle:mbdata:fuelstatus%20mb:vehicle:mbdata:vehiclestatus%20mb:vehicle:mbdata:vehiclelock%20offline_access" -URL_OAUTH = "https://id.mercedes-benz.com/as/token.oauth2" +URL_OAUTH_BASE = "https://id.mercedes-benz.com/as" +URL_OAUTH_AUTH = URL_OAUTH_BASE + "/authorization.oauth2?response_type=code" +URL_OAUTH_TOKEN = URL_OAUTH_BASE + "/token.oauth2" URL_RES_PREFIX = "https://api.mercedes-benz.com/vehicledata/v2" # File Parameters diff --git a/custom_components/mercedesmeapi/config.py b/custom_components/mercedesmeapi/config.py index c91f034..86368fe 100644 --- a/custom_components/mercedesmeapi/config.py +++ b/custom_components/mercedesmeapi/config.py @@ -59,8 +59,6 @@ class MercedesMeConfig: access_token = "" refresh_token = "" token_expires_in = "" - oauth_url = URL_OAUTH - res_url_prefix = URL_RES_PREFIX ######################## # Init @@ -137,6 +135,9 @@ class MercedesMeConfig: # Check Token ######################## def CheckToken(self, token): + if token == None: + _LOGGER.error ("Error reading token.") + return False if "error" in token: if "error_description" in token: _LOGGER.error ("Error retriving token: " + token["error_description"]) @@ -158,8 +159,16 @@ class MercedesMeConfig: # Create Token ######################## def CreateToken(self): + + auth_url = ( + f"{URL_OAUTH_AUTH}&" + + f"client_id={self.client_id}&" + + f"redirect_uri={REDIRECT_URL}&" + + f"scope={SCOPE}" + ) + print( "Open the browser and insert this link:\n" ) - print( "https://id.mercedes-benz.com/as/authorization.oauth2?response_type=code&client_id=" + self.client_id + "&redirect_uri=" + REDIRECT_URL + "&scope=" + SCOPE + "\n") + print(auth_url + "\n") print( "Copy the code in the url:") auth_code = input() diff --git a/custom_components/mercedesmeapi/const.py b/custom_components/mercedesmeapi/const.py index 9155f2d..6d1a20c 100644 --- a/custom_components/mercedesmeapi/const.py +++ b/custom_components/mercedesmeapi/const.py @@ -15,7 +15,9 @@ CREDENTIAL_FILE = ".mercedesme_credentials" RESOURCES_FILE = ".mercedesme_resources" REDIRECT_URL = "https://localhost" SCOPE = "mb:vehicle:mbdata:fuelstatus%20mb:vehicle:mbdata:vehiclestatus%20mb:vehicle:mbdata:vehiclelock%20offline_access" -URL_OAUTH = "https://id.mercedes-benz.com/as/token.oauth2" +URL_OAUTH_BASE = "https://id.mercedes-benz.com/as" +URL_OAUTH_AUTH = URL_OAUTH_BASE + "/authorization.oauth2?response_type=code" +URL_OAUTH_TOKEN = URL_OAUTH_BASE + "/token.oauth2" URL_RES_PREFIX = "https://api.mercedes-benz.com/vehicledata/v2" #UPDATE_SIGNAL = "mercedesmeapi_update" diff --git a/custom_components/mercedesmeapi/query.py b/custom_components/mercedesmeapi/query.py index 3e2a24a..2fde861 100644 --- a/custom_components/mercedesmeapi/query.py +++ b/custom_components/mercedesmeapi/query.py @@ -77,7 +77,7 @@ def GetToken(config, refresh=True, auth_code=""): # Refresh data = "grant_type=refresh_token&refresh_token=" + config.refresh_token - res = requests.post(URL_OAUTH, data = data, headers = headers) + res = requests.post(URL_OAUTH_TOKEN, data = data, headers = headers) try: token = res.json() except ValueError: diff --git a/query.py b/query.py index ea51a46..39799b0 100644 --- a/query.py +++ b/query.py @@ -77,7 +77,7 @@ def GetToken(config, refresh=True, auth_code=""): # Refresh data = "grant_type=refresh_token&refresh_token=" + config.refresh_token - res = requests.post(URL_OAUTH, data = data, headers = headers) + res = requests.post(URL_OAUTH_TOKEN, data = data, headers = headers) try: token = res.json() except ValueError: