Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/tennistv.py (Python)

Code before:

            login_page = self._download_webpage(BASE_URL, None, query={
                'client_id': 'tennis-tv-web',
                'redirect_uri': 'https://tennistv.com',
                'response_mode': 'fragment',
                'response_type': 'code',
                'scope': 'openid'
            })
            post_url = self._html_search_regex(r'action=["\'](.+?)["\']\s+?method=["\']post["\']', login_page, None)

            temp_page = self._download_webpage(post_url, None, 'Sending login data', 'Unable to send login data',
                                               data=urlencode_postdata(login_form), headers=self.headers)

            if 'Your username or password was incorrect' in temp_page:
                raise ExtractorError('Your username or password was incorrect', expected=True)

            handle = self._request_webpage(update_url_query(BASE_URL, query={
                'client_id': 'tennis-tv-web',
                'redirect_uri': 'https://www.tennistv.com/resources/v1.1.10/html/silent-check-sso.html',
                'state': random_uuidv4(),
                'response_mode': 'fragment',
                'response_type': 'code',
                'scope': 'openid',
                'nonce': random_uuidv4(),
                'prompt': 'none'}),
                '', headers=self.headers)

            self.get_token(None, {
                'code': compat_urlparse.parse_qs(handle.geturl()).get('code')[-1],  # There can be multiple, always use the last one
                'grant_type': 'authorization_code',
                'client_id': 'tennis-tv-web',
                'redirect_uri': 'https://www.tennistv.com/resources/v1.1.10/html/silent-check-sso.html'
            })
            return

        if cookies.get('access_token') and cookies.get('refresh_token') is not None:
            self.ACCESS_TOKEN = cookies.get('access_token').value
            self.REFRESH_TOKEN = cookies.get('refresh_token').value
            return

        else:
            self.report_warning('Site requires a subscribed account. Either pass in the cookies or login credentials.')

    def get_token(self, video_id, payload):
        res = self._download_json('https://sso.tennistv.com/auth/realms/TennisTV/protocol/openid-connect/token', video_id,
                                  'Fetching tokens', 'Unable to fetch tokens', headers=self.headers,
                                  data=urlencode_postdata(payload))

        self.ACCESS_TOKEN = res.get('access_token')
        self.REFRESH_TOKEN = res.get('refresh_token')


Review comment (suggestion):
```py
            handle = self._request_webpage(BASE_URL, query=..., headers=self.headers)
```

Code after:
                'scope': 'openid'
            })

        post_url = self._html_search_regex(r'action=["\']([^"\']+?)["\']\s+method=["\']post["\']', login_page, 'login POST url')
        temp_page = self._download_webpage(
            post_url, None, 'Sending login data', 'Unable to send login data',
            headers=self._HEADERS, data=urlencode_postdata({
                'username': username,
                'password': password,
                'submitAction': 'Log In'
            }))
        if 'Your username or password was incorrect' in temp_page:
            raise ExtractorError('Your username or password was incorrect', expected=True)

        handle = self._request_webpage(
            f'{self._AUTH_BASE_URL}/auth', None, 'Logging in', headers=self._HEADERS,
            query={
                'client_id': 'tennis-tv-web',
                'redirect_uri': 'https://www.tennistv.com/resources/v1.1.10/html/silent-check-sso.html',
                'state': random_uuidv4(),
                'response_mode': 'fragment',
                'response_type': 'code',
                'scope': 'openid',
                'nonce': random_uuidv4(),
                'prompt': 'none'
            })

        self.get_token(None, {
            'code': urllib.parse.parse_qs(handle.geturl())['code'][-1],
            'grant_type': 'authorization_code',
            'client_id': 'tennis-tv-web',
            'redirect_uri': 'https://www.tennistv.com/resources/v1.1.10/html/silent-check-sso.html'
        })

    def get_token(self, video_id, payload):
        res = self._download_json(
            f'{self._AUTH_BASE_URL}/token', video_id, 'Fetching tokens',
            'Unable to fetch tokens', headers=self._HEADERS, data=urlencode_postdata(payload))

        self.access_token = res.get('access_token') or self.access_token
        self.refresh_token = res.get('refresh_token') or self.refresh_token

    def _real_initialize(self):
        if self.access_token and self.refresh_token:
            return

        cookies = self._get_cookies('https://www.tennistv.com/')
        if not cookies.get('access_token') or not cookies.get('refresh_token'):
            self.raise_login_required()
        self.access_token, self.refresh_token = cookies['access_token'].value, cookies['refresh_token'].value