From c6b77f19f0861ee97578a3988b7bb5ad3548b4a0 Mon Sep 17 00:00:00 2001 From: Michael Arthur Date: Wed, 28 Aug 2024 19:31:47 +1200 Subject: [PATCH] fix issues with some aliyun servers for url encoded loginbyoauth not working as expected --- pymammotion/aliyun/cloud_gateway.py | 20 ++++++++++++------- .../dataclass/login_by_oauth_response.py | 2 +- pymammotion/const.py | 4 ++-- pymammotion/http/http.py | 2 +- pyproject.toml | 4 ++-- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/pymammotion/aliyun/cloud_gateway.py b/pymammotion/aliyun/cloud_gateway.py index 0b71eef..c4a04ab 100644 --- a/pymammotion/aliyun/cloud_gateway.py +++ b/pymammotion/aliyun/cloud_gateway.py @@ -11,6 +11,7 @@ import uuid from logging import getLogger, exception from datetime import datetime +from urllib.parse import urlencode from aiohttp import ClientSession from alibabacloud_iot_api_gateway.client import Client @@ -241,6 +242,7 @@ def aep_handle(self): async def connect(self): """Connect to the Aliyun Cloud IoT Gateway.""" region_url = "sdk.openaccount.aliyun.com" + time_now = time.time() async with ClientSession() as session: headers = { "host": region_url, @@ -323,7 +325,7 @@ async def login_by_oauth(self, country_code: str, auth_code: str): "x-ca-key": self._app_key, "x-ca-signaturemethod": "HmacSHA256", "accept": "application/json", - "content-type": "application/x-www-form-urlencoded", + "content-type": "application/x-www-form-urlencoded; charset=utf-8", "user-agent": UtilClient.get_user_agent(None), "vid": self._connect_response.data.vid, } @@ -331,14 +333,16 @@ async def login_by_oauth(self, country_code: str, auth_code: str): _bodyParam = { "country": country_code, "authCode": auth_code, - "oauthPlateform": "23", + "oauthPlateform": 23, "oauthAppKey": self._app_key, - "appAuthToken": self._device_sn, "riskControlInfo": { "appID": "com.agilexrobotics", + "appAuthToken": "", "signType": "RSA", + "sdkVersion": "3.4.2", "utdid": self._utdid, "umidToken": self._utdid, + "deviceId": self._connect_response.data.data.device.data.deviceId, "USE_OA_PWD_ENCRYPT": "true", "USE_H5_NC": "true", }, @@ -354,12 +358,12 @@ async def login_by_oauth(self, country_code: str, auth_code: str): header = "".join(f"{k}:{dic[k]}\n" for k in keys).strip() headers["x-ca-signature-headers"] = sign_headers - string_to_sign = "POST\n{}\n\n{}\n{}\n{}\n/api/prd/loginbyoauth.json?loginByOauthRequest={}".format( + string_to_sign = "POST\n{}\n\n{}\n{}\n{}\n/api/prd/loginbyoauth.json?{}".format( headers["accept"], headers["content-type"], headers["date"], header, - json.dumps(_bodyParam, separators=(",", ":")), + f"loginByOauthRequest={json.dumps(_bodyParam, separators=(",", ":"))}", ) hash_val = hmac.new( @@ -369,12 +373,14 @@ async def login_by_oauth(self, country_code: str, auth_code: str): ).digest() signature = base64.b64encode(hash_val).decode("utf-8") headers["x-ca-signature"] = signature - async with session.post( f"https://{region_url}/api/prd/loginbyoauth.json", headers=headers, - params={"loginByOauthRequest": json.dumps(_bodyParam, separators=(",", ":"))}, + data={ + 'loginByOauthRequest': json.dumps(_bodyParam, separators=(",", ":")) + } ) as resp: + data = await resp.json() logger.debug(data) if resp.status == 200: diff --git a/pymammotion/aliyun/dataclass/login_by_oauth_response.py b/pymammotion/aliyun/dataclass/login_by_oauth_response.py index 8adc26b..b00e540 100644 --- a/pymammotion/aliyun/dataclass/login_by_oauth_response.py +++ b/pymammotion/aliyun/dataclass/login_by_oauth_response.py @@ -53,8 +53,8 @@ class InnerData(DataClassORJSONMixin): data: InnerDataContent subCode: int message: str - deviceId: str successful: str + deviceId: Optional[str] = None @dataclass diff --git a/pymammotion/const.py b/pymammotion/const.py index 31f770f..dee0cbe 100644 --- a/pymammotion/const.py +++ b/pymammotion/const.py @@ -2,8 +2,8 @@ APP_KEY = "34231230" APP_SECRET = "1ba85698bb10e19c6437413b61ba3445" -APP_VERSION = "1.11.1220" +APP_VERSION = "1.11.130" ALIYUN_DOMAIN = "api.link.aliyun.com" -MAMMOTION_DOMAIN = "https://domestic.mammotion.com" +MAMMOTION_DOMAIN = "https://id.mammotion.com" MAMMOTION_CLIENT_ID = "MADKALUBAS" MAMMOTION_CLIENT_SECRET = "GshzGRZJjuMUgd2sYHM7" diff --git a/pymammotion/http/http.py b/pymammotion/http/http.py index 7888308..d7fc4cf 100644 --- a/pymammotion/http/http.py +++ b/pymammotion/http/http.py @@ -55,7 +55,7 @@ def __init__(self, login: LoginResponseData): @classmethod async def login(cls, session: ClientSession, username: str, password: str) -> Response[LoginResponseData]: async with session.post( - "/user-server/v1/user/oauth/token", + "/oauth/token", params=dict( username=username, password=password, diff --git a/pyproject.toml b/pyproject.toml index 6f34550..c21d78d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,10 +1,10 @@ [project] name = "pymammotion" -version = "0.2.17" +version = "0.2.18" [tool.poetry] name = "pymammotion" -version = "0.2.17" +version = "0.2.18" license = "GNU-3.0" description = "" readme = "README.md"