Skip to content

Commit

Permalink
Some minor changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Aug 15, 2024
1 parent b9b2dac commit 3358633
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public YoutubeAudioSourceManager(YoutubeSourceOptions options,
this.allowDirectPlaylistIds = options.isAllowDirectPlaylistIds();
this.clients = clients;
this.cipherManager = new SignatureCipherManager();

this.oauth2Handler = new YoutubeOauth2Handler(httpInterfaceManager);

contextFilter = new YoutubeHttpContextFilter();
Expand Down Expand Up @@ -379,6 +378,11 @@ public YoutubeHttpContextFilter getContextFilter() {
return contextFilter;
}

@NotNull
public YoutubeOauth2Handler getOauth2Handler() {
return oauth2Handler;
}

@NotNull
public HttpInterfaceManager getHttpInterfaceManager() {
return httpInterfaceManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ public void setRefreshToken(@Nullable String refreshToken, boolean skipInitializ

// if refreshAccessToken() fails, enabled will never be flipped, so we don't use
// oauth tokens erroneously.
// TODO?: This should fall back to access token initialization if refresh isn't valid,
// provided "skipInitialization" is not true.
enabled = true;
} else if (!skipInitialization) {
return;
}

if (!skipInitialization) {
initializeAccessToken();
}
}
Expand Down Expand Up @@ -189,24 +190,28 @@ private void pollForToken(String deviceCode, long interval) {
* @param force Whether to forcefully renew the access token, even if it doesn't necessarily
* need to be refreshed yet.
*/
private void refreshAccessToken(boolean force) {
if (!shouldRefreshAccessToken() && !force) {
return;
}
public void refreshAccessToken(boolean force) {
log.debug("Refreshing access token (force: {})", force);

if (DataFormatTools.isNullOrEmpty(refreshToken)) {
throw new IllegalStateException("Cannot fetch access token without a refresh token!");
}

synchronized (this) {
if (!shouldRefreshAccessToken() && !force) {
return;
}
if (!shouldRefreshAccessToken() && !force) {
log.debug("Access token does not need to be refreshed yet.");
return;
}

synchronized (this) {
if (DataFormatTools.isNullOrEmpty(refreshToken)) {
throw new IllegalStateException("Cannot fetch access token without a refresh token!");
}

if (!shouldRefreshAccessToken() && !force) {
log.debug("Access token does not need to be refreshed yet.");
return;
}

// @formatter:off
String requestJson = JsonWriter.string()
.object()
Expand Down Expand Up @@ -260,8 +265,8 @@ public void applyToken(HttpUriRequest request) {
try {
refreshAccessToken(false);
} catch (Throwable t) {
if (fetchErrorLogCount++ <= 3) {
// log fetch errors up to 3 times to avoid spamming logs. in theory requests can still be made
if (++fetchErrorLogCount <= 3) {
// log fetch errors up to 3 consecutive times to avoid spamming logs. in theory requests can still be made
// without an access token, but they are less likely to succeed. regardless, we shouldn't bloat a
// user's logs just in case YT changed something and broke oauth integration.
log.error("Refreshing YouTube access token failed", t);
Expand All @@ -273,6 +278,8 @@ public void applyToken(HttpUriRequest request) {
tokenExpires = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(15);
return;
}

fetchErrorLogCount = 0;
}

// check again to ensure updating worked as expected.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Map<String, String> getYoutubeConfig() {

@PostMapping("/v4/youtube")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void updateRefreshToken(@RequestBody YoutubeOauthConfig config) {
public void updateOauth(@RequestBody YoutubeOauthConfig config) {
YoutubeAudioSourceManager source = playerManager.source(YoutubeAudioSourceManager.class);

if (source == null) {
Expand All @@ -48,4 +48,6 @@ public void updateRefreshToken(@RequestBody YoutubeOauthConfig config) {
source.useOauth2(config.getRefreshToken(), config.getSkipInitialization());
log.debug("Updated YouTube OAuth2 refresh token to {}", config.getRefreshToken());
}


}

0 comments on commit 3358633

Please sign in to comment.