Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update auth headers and payload to get around 401s #188

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ In addition, there is also a [HACS myQ component](https://github.com/ehendrix23/

## Installation

```python
```shell
pip install pymyq
```

Expand Down
2 changes: 1 addition & 1 deletion pymyq/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Define a version constant."""
__version__ = "3.1.6"
__version__ = "3.1.7"
21 changes: 12 additions & 9 deletions pymyq/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Define the MyQ API."""
import asyncio
from datetime import datetime, timedelta
import base64
import logging
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Tuple, Union
from urllib.parse import parse_qs, urlsplit

Expand All @@ -17,12 +18,16 @@
OAUTH_AUTHORIZE_URI,
OAUTH_BASE_URI,
OAUTH_CLIENT_ID,
OAUTH_CLIENT_SECRET,
OAUTH_REDIRECT_URI,
OAUTH_TOKEN_URI,
)
from .device import MyQDevice
from .errors import AuthenticationError, InvalidCredentialsError, MyQError, RequestError
from .errors import (
AuthenticationError,
InvalidCredentialsError,
MyQError,
RequestError,
)
from .garagedoor import MyQGaragedoor
from .lamp import MyQLamp
from .lock import MyQLock
Expand Down Expand Up @@ -414,24 +419,22 @@ async def _oauth_authenticate(self) -> Tuple[str, int]:
_LOGGER.debug("Getting token")
redirect_url = f"{OAUTH_BASE_URI}{resp.headers['Location']}"

get_token_basic_auth = base64.b64encode(f"{OAUTH_CLIENT_ID}:".encode("ascii")).decode("ascii")
resp, data = await self.request(
returns="json",
method="post",
url=OAUTH_TOKEN_URI,
websession=session,
headers={
"Authorization": f"Basic {get_token_basic_auth}",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "*/*",
},
data={
"client_id": OAUTH_CLIENT_ID,
"client_secret": OAUTH_CLIENT_SECRET,
"code": parse_qs(urlsplit(redirect_url).query).get("code", ""),
"code": parse_qs(urlsplit(redirect_url).query).get("code", "")[0],
"code_verifier": self._code_verifier,
"grant_type": "authorization_code",
"redirect_uri": OAUTH_REDIRECT_URI,
"scope": parse_qs(urlsplit(redirect_url).query).get(
"code", "MyQ_Residential offline_access"
),
},
login_request=True,
)
Expand Down
7 changes: 3 additions & 4 deletions pymyq/const.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""The myq constants."""

OAUTH_CLIENT_ID = "IOS_CGI_MYQ"
OAUTH_CLIENT_SECRET = "VUQ0RFhuS3lQV3EyNUJTdw=="
OAUTH_CLIENT_ID = "ANDROID_CGI_MYQ"
OAUTH_BASE_URI = "https://partner-identity.myq-cloud.com"
OAUTH_AUTHORIZE_URI = f"{OAUTH_BASE_URI}/connect/authorize"
OAUTH_REDIRECT_URI = "com.myqops://ios"
OAUTH_REDIRECT_URI = "com.myqops://android"
OAUTH_TOKEN_URI = f"{OAUTH_BASE_URI}/connect/token"

ACCOUNTS_ENDPOINT = "https://accounts.myq-cloud.com/api/v6.0/accounts"
DEVICES_ENDPOINT = (
"https://devices.myq-cloud.com/api/v5.2/Accounts/{account_id}/Devices"
"https://devices.myq-cloud.com/api/v6.0/Accounts/{account_id}/Devices"
)

WAIT_TIMEOUT = 60
Expand Down