]> git.giorgioravera.it Git - mercedes_me_api.git/commitdiff
Separated URLs for oauth2
authorGiorgio Ravera <giorgio.ravera@gmail.com>
Fri, 30 Oct 2020 14:42:25 +0000 (15:42 +0100)
committerGiorgio Ravera <giorgio.ravera@gmail.com>
Fri, 30 Oct 2020 14:42:25 +0000 (15:42 +0100)
config.py
const.py
custom_components/mercedesmeapi/config.py
custom_components/mercedesmeapi/const.py
custom_components/mercedesmeapi/query.py
query.py

index ba3c6d753ab5750928ffb2e3859f916390454379..e9b167a27d4aa459bc15b993d754a5b0dc4d1309 100644 (file)
--- 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()
 
index 4e71886f20aad46dd12c81712b6152895263e421..e6136e067c29a8f80d93a1bca722889bfa07c111 100644 (file)
--- 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
index c91f034d858e4a70a2d10ea71d85eeff74a66435..86368fea2d811f3f0c9c92a9a8ee05d549ca8981 100644 (file)
@@ -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()
 
index 9155f2d2a683be723a87cfdef45c93d560405727..6d1a20cea43b82af8728b59d11231dc0b5b6dd53 100644 (file)
@@ -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"
 
index 3e2a24a4e85293fcd83ae504baf2ca5e75ed924c..2fde861a9160eddcc9e8348a8bc77cabddbb3d33 100644 (file)
@@ -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:
index ea51a461b810fdb7e493b0db78091a566464f373..39799b07f3507c764445ac3e7bb06ded0c632cea 100644 (file)
--- 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: