Skip to content

Commit c53fc49

Browse files
committed
feat: expand on the idea of Teekeks#307 and also make it possible to specify a callback function that handles the output of the authentication url to be opened
1 parent db1c5e3 commit c53fc49

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

twitchAPI/oauth.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ async def authenticate(self,
433433
user_token: Optional[str] = None,
434434
browser_name: Optional[str] = None,
435435
browser_new: int = 2,
436-
use_browser: bool = True):
436+
use_browser: bool = True,
437+
auth_url_callback: Optional[Callable[[str], Awaitable[None]]] = None):
437438
"""Start the user authentication flow\n
438439
If callback_func is not set, authenticate will wait till the authentication process finished and then return
439440
the access_token and the refresh_token
@@ -447,8 +448,14 @@ async def authenticate(self,
447448
:param browser_new: controls in which way the link will be opened in the browser.
448449
See `the webbrowser documentation <https://docs.python.org/3/library/webbrowser.html#webbrowser.open>`__ for more info
449450
|default|:code:`2`
450-
:param use_browser: controls if a browser should be opened by default or if the url to authenticate via should printed to the log
451-
|default|:code: True
451+
:param use_browser: controls if a browser should be opened.
452+
If set to :const:`False`, the browser will not be opened and the URL to be opened will either be printed to the info log or
453+
send to the specified callback function (controlled by :const:`~twitchAPI.oauth.UserAuthenticator.authenticate.params.auth_url_callback`)
454+
|default|:code:`True`
455+
:param auth_url_callback: a async callback that will be called with the url to be used for the authentication flow should
456+
:const:`~twitchAPI.oauth.UserAuthenticator.authenticate.params.use_browser` be :const:`False`.
457+
If left as None, the URL will instead be printed to the info log
458+
|default|:code:`None`
452459
:return: None if callback_func is set, otherwise access_token and refresh_token
453460
:raises ~twitchAPI.type.TwitchAPIException: if authentication fails
454461
:rtype: None or (str, str)
@@ -468,7 +475,10 @@ async def authenticate(self,
468475
browser = webbrowser.get(browser_name)
469476
browser.open(self._build_auth_url(), new=browser_new)
470477
else:
471-
self.logger.info(f"To authenticate open: {self._build_auth_url()}")
478+
if auth_url_callback is not None:
479+
await auth_url_callback(self._build_auth_url())
480+
else:
481+
self.logger.info(f"To authenticate open: {self._build_auth_url()}")
472482
while self._user_token is None:
473483
await asyncio.sleep(0.01)
474484
# now we need to actually get the correct token

0 commit comments

Comments
 (0)