Skip to content

Commit

Permalink
Fix redirect URIs issue
Browse files Browse the repository at this point in the history
Closes #33.
  • Loading branch information
parafoxia committed Apr 11, 2022
1 parent 00d7182 commit d25fe42
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion analytix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
)

__productname__ = "analytix"
__version__ = "3.3.1"
__version__ = "3.3.2"
__description__ = "A simple yet powerful wrapper for the YouTube Analytics API."
__url__ = "https://github.com/parafoxia/analytix"
__docs__ = "https://analytix.readthedocs.io"
Expand Down
12 changes: 12 additions & 0 deletions analytix/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import os
import pathlib
import typing as t
from warnings import warn

import httpx

Expand Down Expand Up @@ -205,6 +206,12 @@ def authorise( # nosec B107
The tokens the client is authorised with.
"""

warning = (
"Code-based authorisation is deprecated due to a breaking change in the "
"Google APIs -- analytix is patching your secrets file where necessary for "
"now, but the authorisation method will change in the next version"
)

if not isinstance(token_path, pathlib.Path):
token_path = pathlib.Path(token_path)

Expand All @@ -220,6 +227,11 @@ def authorise( # nosec B107
if not self._tokens:
log.info("Unable to load tokens; you will need to authorise")
self._tokens = self._retrieve_tokens()
# Temporary warning regarding authorisation methods.
if log.hasHandlers() and log.getEffectiveLevel() <= 30:
log.warning(warning)
else:
warn(warning)
self._tokens.write(token_path)

log.info("Authorisation complete!")
Expand Down
12 changes: 12 additions & 0 deletions analytix/async_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import os
import pathlib
import typing as t
from warnings import warn

import httpx

Expand Down Expand Up @@ -208,6 +209,12 @@ async def authorise( # nosec B107
The tokens the client is authorised with.
"""

warning = (
"Code-based authorisation is deprecated due to a breaking change in the "
"Google APIs -- analytix is patching your secrets file where necessary for "
"now, but the authorisation method will change in the next version"
)

if not isinstance(token_path, pathlib.Path):
token_path = pathlib.Path(token_path)

Expand All @@ -223,6 +230,11 @@ async def authorise( # nosec B107
if not self._tokens:
log.info("Unable to load tokens; you will need to authorise")
self._tokens = await self._retrieve_tokens()
# Temporary warning regarding authorisation methods.
if log.hasHandlers() and log.getEffectiveLevel() <= 30:
log.warning(warning)
else:
warn(warning)
await self._tokens.awrite(token_path)

log.info("Authorisation complete!")
Expand Down
8 changes: 8 additions & 0 deletions analytix/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def from_file(cls, path: pathlib.Path | str) -> Secrets:
with open(path) as f:
data = json.load(f)["installed"]

# Temporary patch to account for the breaking change in the API.
if "urn:ietf:wg:oauth:2.0:oob" not in data["redirect_uris"]:
data["redirect_uris"].insert(0, "urn:ietf:wg:oauth:2.0:oob")

log.info("Secrets loaded!")
return cls(**data)

Expand All @@ -127,6 +131,10 @@ async def afrom_file(cls, path: pathlib.Path | str) -> Secrets:
async with aiofiles.open(path) as f:
data = json.loads(await f.read())["installed"]

# Temporary patch to account for the breaking change in the API.
if "urn:ietf:wg:oauth:2.0:oob" not in data["redirect_uris"]:
data["redirect_uris"].insert(0, "urn:ietf:wg:oauth:2.0:oob")

log.info("Secrets loaded!")
return cls(**data)

Expand Down

0 comments on commit d25fe42

Please sign in to comment.