From 56fa4f9f8625ae73a09e832f15556008b398a394 Mon Sep 17 00:00:00 2001 From: Giorgio Ravera Date: Sat, 9 Jan 2021 22:02:52 +0100 Subject: [PATCH] Fixed readme & minor fixes --- README.md | 13 +++++++++++- config.py | 2 -- const.py | 2 +- custom_components/mercedesmeapi/const.py | 2 +- custom_components/mercedesmeapi/oauth.py | 22 ++++++++------------ custom_components/mercedesmeapi/resources.py | 4 ++++ info.md | 13 +++++++++++- mercedes_me_api.py | 9 ++++---- oauth.py | 21 ++++++++----------- resources.py | 4 ++++ 10 files changed, 57 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 7e0f514..f74c281 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This repository contains a collection of scripts (including the Home Assistant C ## Pre-Requirements 1) Own a Mercedes Benz Car with Mercedes me installed and working. 2) Create an application in https://developer.mercedes-benz.com/ -3) Register to the following APIs: +3) Register to the following APIs (all of them): - [Electric Vehicle Status](https://developer.mercedes-benz.com/products/electric_vehicle_status) - [Fuel Status](https://developer.mercedes-benz.com/products/fuel_status) - [Pay As You Drive Insurance](https://developer.mercedes-benz.com/products/pay_as_you_drive_insurance) @@ -78,6 +78,17 @@ ENABLE_RESOURCES_FILE: <**ENABLE (true) OR DISABLE (false) RESOURCES - (DEFAULT where CLIENT_ID and CLIENT_SECRET referring to the application information that can be found in [Mercedes Developer Console](https://developer.mercedes-benz.com/console) and VEHICLE_ID is the VIN of your car. +## First Token Creation +To create the first token it's necessary to perform the following steps: +1) Create the APP and register it to all APIs as describe in the Pre-Requirements paragraph +2) Logout in all browser from Mercedes Me (Developer site included) +3) Execute the shell or python script with -t parameter to obrain the first token +4) Open with a browser the link provided by the script: a login page will be shown +5) Using your Mercedes Me credential log in: a new web-page will ask to authorize the APP created in the step 1 to access to the personal information associated to the APIs registered +6) Enable all information and press Allow +7) An error page will appear: check the URI: it's something like https://localhost/?code=DQ8htZSw4WtJ27r7sTrVJwszGWxrCx9emy5FDUFa +8) Copy the part of the URI after code= (in this case DQ8htZSw4WtJ27r7sTrVJwszGWxrCx9emy5FDUFa) and paste into the running script + ### Python Usage To execute the script read below: ```bash diff --git a/config.py b/config.py index b449f97..045577d 100644 --- a/config.py +++ b/config.py @@ -30,8 +30,6 @@ class MercedesMeConfig: # Read Configuration ######################## def ReadConfig(self): - needToRefresh = False - # Read Config from file if not os.path.isfile(self.config_file): _LOGGER.error(f"Credential File {self.config_file} not found") diff --git a/const.py b/const.py index 42a5ff7..7967c05 100644 --- a/const.py +++ b/const.py @@ -9,7 +9,7 @@ https://github.com/xraver/mercedes_me_api/ # Software Name & Version NAME = "Mercedes Me API" DOMAIN = "mercedesmeapi" -VERSION = "0.10" +VERSION = "0.11" # Software Parameters TOKEN_FILE = ".mercedesme_token" CONFIG_FILE = ".mercedesme_config" diff --git a/custom_components/mercedesmeapi/const.py b/custom_components/mercedesmeapi/const.py index e489f02..f9cbe81 100644 --- a/custom_components/mercedesmeapi/const.py +++ b/custom_components/mercedesmeapi/const.py @@ -9,7 +9,7 @@ https://github.com/xraver/mercedes_me_api/ # Software Name & Version NAME = "Mercedes Me API" DOMAIN = "mercedesmeapi" -VERSION = "0.10" +VERSION = "0.11" # Software Parameters TOKEN_FILE = ".mercedesme_token" RESOURCES_FILE = ".mercedesme_resources" diff --git a/custom_components/mercedesmeapi/oauth.py b/custom_components/mercedesmeapi/oauth.py index 94c210b..3e34214 100644 --- a/custom_components/mercedesmeapi/oauth.py +++ b/custom_components/mercedesmeapi/oauth.py @@ -56,12 +56,11 @@ class MercedesMeOauth: def ReadToken(self): found = False - needToRefresh = False # Read Token if not os.path.isfile(self.token_file): - # Token File not present - Creating new one - _LOGGER.error("Token File missing - Creating a new one") + # Token File not present + _LOGGER.error("Token File missing") found = False else: with open(self.token_file, "r") as file: @@ -72,23 +71,16 @@ class MercedesMeOauth: else: found = True except ValueError: - _LOGGER.error("Error reading token file - Creating a new one") + _LOGGER.error("Error reading token file") found = False - if not found: - # Not valid or file missing - #if (not self.CreateToken()): GRGR -> to be fixed - if (True): - _LOGGER.error("Error creating token") - return False - else: + if found: # Valid: just import self.access_token = token["access_token"] self.refresh_token = token["refresh_token"] self.token_expires_in = token["expires_in"] - needToRefresh = True - if needToRefresh: + # Refreshing if not self.RefreshToken(): _LOGGER.error("Error refreshing token") return False @@ -164,6 +156,10 @@ class MercedesMeOauth: ######################## def RefreshToken(self): + if self.access_token == "": + _LOGGER.error("Error: Token not found or not valid") + return False + data = f"grant_type=refresh_token&refresh_token={self.refresh_token}" token = GetToken(URL_OAUTH_TOKEN, self.headers, data, refresh=True) diff --git a/custom_components/mercedesmeapi/resources.py b/custom_components/mercedesmeapi/resources.py index cf5f23a..9331753 100644 --- a/custom_components/mercedesmeapi/resources.py +++ b/custom_components/mercedesmeapi/resources.py @@ -111,6 +111,10 @@ class MercedesMeResources: found = False resources = None + if self.mercedesConfig.token.access_token == "": + _LOGGER.error("Error: Token not found or not valid") + return False + if self.mercedesConfig.enable_resources_file == True: if not os.path.isfile(self.resources_file): # Resources File not present - Retrieving new one from server diff --git a/info.md b/info.md index 88dbb9e..e548320 100644 --- a/info.md +++ b/info.md @@ -11,7 +11,7 @@ This component is still in development. ## Pre-Requirements 1) Own a Mercedes Benz Car with Mercedes me installed and working. 2) Create an application in https://developer.mercedes-benz.com/ -3) Register to the following APIs: +3) Register to the following APIs (all of them): - [Electric Vehicle Status](https://developer.mercedes-benz.com/products/electric_vehicle_status) - [Fuel Status](https://developer.mercedes-benz.com/products/fuel_status) - [Pay As You Drive Insurance](https://developer.mercedes-benz.com/products/pay_as_you_drive_insurance) @@ -59,6 +59,17 @@ logger: custom_components.mercedesmeapi: debug ``` +## First Token Creation +To create the first token it's necessary to perform the following steps: +1) Create the APP and register it to all APIs as describe in the Pre-Requirements paragraph +2) Logout in all browser from Mercedes Me (Developer site included) +3) Execute the shell or python script with -t parameter to obrain the first token +4) Open with a browser the link provided by the script: a login page will be shown +5) Using your Mercedes Me credential log in: a new web-page will ask to authorize the APP created in the step 1 to access to the personal information associated to the APIs registered +6) Enable all information and press Allow +7) An error page will appear: check the URI: it's something like https://localhost/?code=DQ8htZSw4WtJ27r7sTrVJwszGWxrCx9emy5FDUFa +8) Copy the part of the URI after code= (in this case DQ8htZSw4WtJ27r7sTrVJwszGWxrCx9emy5FDUFa) and paste into the running script + ## Change Log You can find change log under [releases][releases-url] diff --git a/mercedes_me_api.py b/mercedes_me_api.py index 4c8d0f3..038eefa 100644 --- a/mercedes_me_api.py +++ b/mercedes_me_api.py @@ -95,12 +95,13 @@ if __name__ == "__main__": exit(1) # Read Resources - if not data.mercedesResources.ReadResources(): - _LOGGER.error("Error initializing resources") - exit(1) + if (args.resources == True) | (args.status == True): + if not data.mercedesResources.ReadResources(): + _LOGGER.error("Error initializing resources") + exit(1) # Print Available Resources - if args.resources: + if args.resources == True: data.mercedesResources.PrintAvailableResources() # Print Resources State diff --git a/oauth.py b/oauth.py index e01b755..ea35f0d 100644 --- a/oauth.py +++ b/oauth.py @@ -56,12 +56,11 @@ class MercedesMeOauth: def ReadToken(self): found = False - needToRefresh = False # Read Token if not os.path.isfile(self.token_file): - # Token File not present - Creating new one - _LOGGER.error("Token File missing - Creating a new one") + # Token File not present + _LOGGER.error("Token File missing") found = False else: with open(self.token_file, "r") as file: @@ -72,22 +71,16 @@ class MercedesMeOauth: else: found = True except ValueError: - _LOGGER.error("Error reading token file - Creating a new one") + _LOGGER.error("Error reading token file") found = False - if not found: - # Not valid or file missing - if not self.CreateToken(): - _LOGGER.error("Error creating token") - return False - else: + if found: # Valid: just import self.access_token = token["access_token"] self.refresh_token = token["refresh_token"] self.token_expires_in = token["expires_in"] - needToRefresh = True - if needToRefresh: + # Refreshing if not self.RefreshToken(): _LOGGER.error("Error refreshing token") return False @@ -163,6 +156,10 @@ class MercedesMeOauth: ######################## def RefreshToken(self): + if self.access_token == "": + _LOGGER.error("Error: Token not found or not valid") + return False + data = f"grant_type=refresh_token&refresh_token={self.refresh_token}" token = GetToken(URL_OAUTH_TOKEN, self.headers, data, refresh=True) diff --git a/resources.py b/resources.py index bd3c56f..7424488 100644 --- a/resources.py +++ b/resources.py @@ -106,6 +106,10 @@ class MercedesMeResources: found = False resources = None + if self.mercedesConfig.token.access_token == "": + _LOGGER.error("Error: Token not found or not valid") + return False + if self.mercedesConfig.enable_resources_file == True: if not os.path.isfile(self.resources_file): # Resources File not present - Retrieving new one from server -- 2.47.3