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

Allow not auto-opening browser for oauth workflow #307

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions twitchAPI/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ async def authenticate(self,
callback_func: Optional[Callable[[str, str], None]] = None,
user_token: Optional[str] = None,
browser_name: Optional[str] = None,
browser_new: int = 2):
browser_new: int = 2,
use_browser: bool = True):
"""Start the user authentication flow\n
If callback_func is not set, authenticate will wait till the authentication process finished and then return
the access_token and the refresh_token
Expand All @@ -358,6 +359,8 @@ async def authenticate(self,
:param browser_new: controls in which way the link will be opened in the browser.
See `the webbrowser documentation <https://docs.python.org/3/library/webbrowser.html#webbrowser.open>`__ for more info
|default|:code:`2`
:param use_browser: controls if a browser should be opened by default or if the url to authenticate via should printed to the log
|default|:code: True
:return: None if callback_func is set, otherwise access_token and refresh_token
:raises ~twitchAPI.type.TwitchAPIException: if authentication fails
:rtype: None or (str, str)
Expand All @@ -372,9 +375,12 @@ async def authenticate(self,
# wait for the server to start up
while not self._server_running:
await asyncio.sleep(0.01)
# open in browser
browser = webbrowser.get(browser_name)
browser.open(self._build_auth_url(), new=browser_new)
if use_browser:
# open in browser
browser = webbrowser.get(browser_name)
browser.open(self._build_auth_url(), new=browser_new)
else:
self.logger.info(f"To authenticate open: {self._build_auth_url()}")
Copy link
Contributor

@Braastos Braastos May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if logging the url is needed, because you can retrieve the url with return_auth_url() method from your own code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the reason why I want to log the url here is that the .authenticate( call is blocking as it is opening a listener for the response from twitch. You are right that the calling code could run .return_auth_url( before calling authenticate to present the url to the user, but I think it is helpful to not require the calling code to know it needs to do so, and logging the data info will usually be visible if the user if running via the CLI.

while self._user_token is None:
await asyncio.sleep(0.01)
# now we need to actually get the correct token
Expand Down