diff --git a/common/src/main/java/dev/lavalink/youtube/YoutubeAudioSourceManager.java b/common/src/main/java/dev/lavalink/youtube/YoutubeAudioSourceManager.java index ac27a5d..5dc62cb 100644 --- a/common/src/main/java/dev/lavalink/youtube/YoutubeAudioSourceManager.java +++ b/common/src/main/java/dev/lavalink/youtube/YoutubeAudioSourceManager.java @@ -212,6 +212,7 @@ protected AudioItem loadItemOnce(@NotNull AudioReference reference) { } log.debug("Attempting to load {} with client \"{}\"", reference.identifier, client.getIdentifier()); + httpInterface.getContext().setAttribute(Client.OAUTH_CLIENT_ATTRIBUTE, client.supportsOAuth()); try { AudioItem item = router.route(client); diff --git a/common/src/main/java/dev/lavalink/youtube/clients/TvHtml5Embedded.java b/common/src/main/java/dev/lavalink/youtube/clients/TvHtml5Embedded.java index 073687b..3fe8694 100644 --- a/common/src/main/java/dev/lavalink/youtube/clients/TvHtml5Embedded.java +++ b/common/src/main/java/dev/lavalink/youtube/clients/TvHtml5Embedded.java @@ -105,6 +105,11 @@ public boolean canHandleRequest(@NotNull String identifier) { return (!identifier.contains("list=") || identifier.contains("list=RD")) && super.canHandleRequest(identifier); } + @Override + public boolean supportsOAuth() { + return true; + } + @Override @NotNull public String getIdentifier() { diff --git a/common/src/main/java/dev/lavalink/youtube/clients/skeleton/Client.java b/common/src/main/java/dev/lavalink/youtube/clients/skeleton/Client.java index 74cf41f..eebc54f 100644 --- a/common/src/main/java/dev/lavalink/youtube/clients/skeleton/Client.java +++ b/common/src/main/java/dev/lavalink/youtube/clients/skeleton/Client.java @@ -25,6 +25,8 @@ * The interface for a Client. */ public interface Client { + String OAUTH_CLIENT_ATTRIBUTE = "yt-oauth-enabled-client"; + String WATCH_URL = "https://www.youtube.com/watch?v="; String API_BASE_URL = "https://youtubei.googleapis.com/youtubei/v1"; String PLAYER_URL = API_BASE_URL + "/player?prettyPrint=false"; @@ -208,6 +210,13 @@ default boolean isEmbedded() { return false; } + /** + * @return True, if this client supports account linking via OAuth (i.e. TV) + */ + default boolean supportsOAuth() { + return false; + } + void setPlaylistPageCount(int count); /** diff --git a/common/src/main/java/dev/lavalink/youtube/http/YoutubeHttpContextFilter.java b/common/src/main/java/dev/lavalink/youtube/http/YoutubeHttpContextFilter.java index 939c73c..03ba527 100644 --- a/common/src/main/java/dev/lavalink/youtube/http/YoutubeHttpContextFilter.java +++ b/common/src/main/java/dev/lavalink/youtube/http/YoutubeHttpContextFilter.java @@ -3,6 +3,7 @@ import com.sedmelluq.discord.lavaplayer.tools.FriendlyException; import com.sedmelluq.discord.lavaplayer.tools.http.HttpContextRetryCounter; import com.sedmelluq.discord.lavaplayer.tools.io.HttpClientTools; +import dev.lavalink.youtube.clients.skeleton.Client; import org.apache.http.HttpResponse; import org.apache.http.client.CookieStore; import org.apache.http.client.methods.HttpUriRequest; @@ -79,7 +80,12 @@ public void onRequest(HttpClientContext context, context.removeAttribute(ATTRIBUTE_USER_AGENT_SPECIFIED); } - oauth2Handler.applyToken(request); + boolean isRequestFromOauthedClient = context.getAttribute(Client.OAUTH_CLIENT_ATTRIBUTE) == Boolean.TRUE; + + if (isRequestFromOauthedClient && Client.PLAYER_URL.equals(request.getURI().toString())) { + // only apply the token to /player requests. + oauth2Handler.applyToken(request); + } } // try { diff --git a/common/src/main/java/dev/lavalink/youtube/http/YoutubeOauth2Handler.java b/common/src/main/java/dev/lavalink/youtube/http/YoutubeOauth2Handler.java index ace3703..7540333 100644 --- a/common/src/main/java/dev/lavalink/youtube/http/YoutubeOauth2Handler.java +++ b/common/src/main/java/dev/lavalink/youtube/http/YoutubeOauth2Handler.java @@ -259,10 +259,6 @@ public void applyToken(HttpUriRequest request) { return; } - if (!Client.PLAYER_URL.equals(request.getURI().toString())) { - return; - } - if (shouldRefreshAccessToken()) { log.debug("Access token has expired, refreshing..."); diff --git a/common/src/main/java/dev/lavalink/youtube/track/YoutubeAudioTrack.java b/common/src/main/java/dev/lavalink/youtube/track/YoutubeAudioTrack.java index 4ad6259..0fe4c1d 100644 --- a/common/src/main/java/dev/lavalink/youtube/track/YoutubeAudioTrack.java +++ b/common/src/main/java/dev/lavalink/youtube/track/YoutubeAudioTrack.java @@ -68,6 +68,8 @@ public void process(LocalAudioTrackExecutor localExecutor) throws Exception { continue; } + httpInterface.getContext().setAttribute(Client.OAUTH_CLIENT_ATTRIBUTE, client.supportsOAuth()); + try { processWithClient(localExecutor, httpInterface, client, 0); return; // stream played through successfully, short-circuit.