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

Code before:
    _OAUTH_TOKEN_ENDPOINT = 'https://oauth2.googleapis.com/token'                       # token_endpoint

    @staticmethod
    def _get_oauth_cache_key(profile):
        return f'oauth_refresh_token_{profile}'

    def _set_oauth_info(self, token_response, profile):
        YoutubeBaseInfoExtractor._OAUTH_ACCESS_TOKEN_CACHE.setdefault(profile, {}).update({
            'access_token': token_response['access_token'],
            'token_type': token_response['token_type'],
            'expiry': time_seconds(
                seconds=traverse_obj(token_response, ('expires_in', {float_or_none}), default=300) - 10),
        })
        refresh_token = traverse_obj(token_response, 'refresh_token', {str})
        if refresh_token:
            self.cache.store(self._NETRC_MACHINE, self._get_oauth_cache_key(profile), refresh_token)
            YoutubeBaseInfoExtractor._OAUTH_ACCESS_TOKEN_CACHE[profile]['refresh_token'] = refresh_token

    def _initialize_oauth(self, user, refresh_token):
        self._OAUTH_PROFILE = user or 'default'

        if self._OAUTH_PROFILE in YoutubeBaseInfoExtractor._OAUTH_ACCESS_TOKEN_CACHE:
            self.write_debug(f'{self._OAUTH_DISPLAY_ID}: Using cached access token for profile "{self._OAUTH_PROFILE}"')
            return

        YoutubeBaseInfoExtractor._OAUTH_ACCESS_TOKEN_CACHE[self._OAUTH_PROFILE] = {}

        if refresh_token == "''":
            refresh_token = None

        # Allow refresh token passed to initialize cache
        if refresh_token:
            self.cache.store(self._NETRC_MACHINE, self._get_oauth_cache_key(self._OAUTH_PROFILE), refresh_token)

        refresh_token = refresh_token or self.cache.load(self._NETRC_MACHINE, self._get_oauth_cache_key(self._OAUTH_PROFILE))
        if refresh_token:
            YoutubeBaseInfoExtractor._OAUTH_ACCESS_TOKEN_CACHE[self._OAUTH_PROFILE]['refresh_token'] = refresh_token
            try:
                token_response = self._refresh_token(refresh_token)
            except ExtractorError as e:
                self.report_warning(f'{self._OAUTH_DISPLAY_ID}: Failed to refresh access token: {e}')
                token_response = self._oauth_authorize
        else:
            token_response = self._oauth_authorize

        self._set_oauth_info(token_response, self._OAUTH_PROFILE)
        self.write_debug(f'{self._OAUTH_DISPLAY_ID}: Logged in using profile "{self._OAUTH_PROFILE}"')

    def _refresh_token(self, refresh_token):
        try:
            token_response = self._download_json(

Review comment (security):
for consistency?
```suggestion
        YoutubeBaseInfoExtractor._OAUTH_PROFILE = user or 'default'

        if YoutubeBaseInfoExtractor._OAUTH_PROFILE in YoutubeBaseInfoExtractor._OAUTH_ACCESS_TOKEN_CACHE:
```

Code after:

    @property
    def _oauth_cache_key(self):
        return f'oauth_refresh_token_{self._OAUTH_PROFILE}'

    def _read_oauth_error_response(self, response):
        return traverse_obj(
            self._webpage_read_content(response, self._OAUTH_TOKEN_ENDPOINT, self._OAUTH_DISPLAY_ID, fatal=False),
            ({json.loads}, 'error', {str}))

    def _set_oauth_info(self, token_response):
        YoutubeBaseInfoExtractor._OAUTH_ACCESS_TOKEN_CACHE.setdefault(self._OAUTH_PROFILE, {}).update({
            'access_token': token_response['access_token'],
            'token_type': token_response['token_type'],
            'expiry': time_seconds(
                seconds=traverse_obj(token_response, ('expires_in', {float_or_none}), default=300) - 10),
        })
        refresh_token = traverse_obj(token_response, ('refresh_token', {str}))
        if refresh_token:
            self.cache.store(self._NETRC_MACHINE, self._oauth_cache_key, refresh_token)
            YoutubeBaseInfoExtractor._OAUTH_ACCESS_TOKEN_CACHE[self._OAUTH_PROFILE]['refresh_token'] = refresh_token

    def _initialize_oauth(self, user, refresh_token):
        self._OAUTH_PROFILE = user or 'default'

        if self._OAUTH_PROFILE in YoutubeBaseInfoExtractor._OAUTH_ACCESS_TOKEN_CACHE:
            self.write_debug(f'{self._OAUTH_DISPLAY_ID}: Using cached access token for profile "{self._OAUTH_PROFILE}"')
            return

        YoutubeBaseInfoExtractor._OAUTH_ACCESS_TOKEN_CACHE[self._OAUTH_PROFILE] = {}

        if refresh_token:
            refresh_token = refresh_token.strip('\'') or None

        # Allow refresh token passed to initialize cache
        if refresh_token:
            self.cache.store(self._NETRC_MACHINE, self._oauth_cache_key, refresh_token)

        refresh_token = refresh_token or self.cache.load(self._NETRC_MACHINE, self._oauth_cache_key)
        if refresh_token:
            YoutubeBaseInfoExtractor._OAUTH_ACCESS_TOKEN_CACHE[self._OAUTH_PROFILE]['refresh_token'] = refresh_token
            try:
                token_response = self._refresh_token(refresh_token)
            except ExtractorError as e:
                error_msg = str(e.orig_msg).replace('Failed to refresh access token: ', '')
                self.report_warning(f'{self._OAUTH_DISPLAY_ID}: Failed to refresh access token: {error_msg}')
                token_response = self._oauth_authorize
        else:
            token_response = self._oauth_authorize

        self._set_oauth_info(token_response)