Skip to content

Commit 27dc1b1

Browse files
committed
[YouTube] Remove usage of API keys for InnerTube requests, bump versions
The API keys are not used anymore by official clients in almost all cases (still used by the Android app until it gets a configuration) for all requests we made. Clients and device OS versions have been bumped to their latest stable version known. Methods and fields related to API keys have been renamed or deleted if they're no longer relevant.
1 parent e380bb4 commit 27dc1b1

10 files changed

Lines changed: 90 additions & 184 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java

Lines changed: 60 additions & 138 deletions
Large diffs are not rendered by default.

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelTabExtractor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.DISABLE_PRETTY_PRINT_PARAMETER;
3030
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.YOUTUBEI_V1_URL;
3131
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse;
32-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getKey;
3332
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
3433
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
3534
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
@@ -422,8 +421,8 @@ private Page getNextPageFrom(final JsonObject continuations,
422421
.done())
423422
.getBytes(StandardCharsets.UTF_8);
424423

425-
return new Page(YOUTUBEI_V1_URL + "browse?key=" + getKey()
426-
+ DISABLE_PRETTY_PRINT_PARAMETER, null, channelIds, null, body);
424+
return new Page(YOUTUBEI_V1_URL + "browse?" + DISABLE_PRETTY_PRINT_PARAMETER, null,
425+
channelIds, null, body);
427426
}
428427

429428
/**

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.YOUTUBEI_V1_URL;
55
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.extractCookieValue;
66
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.extractPlaylistTypeFromPlaylistId;
7-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getKey;
87
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody;
98
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getYouTubeHeaders;
109
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
@@ -103,8 +102,8 @@ public void onFetchPage(@Nonnull final Downloader downloader)
103102
final var headers = getYouTubeHeaders();
104103

105104
final Response response = getDownloader().postWithContentTypeJson(
106-
YOUTUBEI_V1_URL + "next?key=" + getKey() + DISABLE_PRETTY_PRINT_PARAMETER,
107-
headers, body, localization);
105+
YOUTUBEI_V1_URL + "next?" + DISABLE_PRETTY_PRINT_PARAMETER, headers, body,
106+
localization);
108107

109108
initialData = JsonUtils.toJsonObject(getValidJsonResponseBody(response));
110109
playlistData = initialData
@@ -225,8 +224,8 @@ private Page getNextPageFrom(@Nonnull final JsonObject playlistJson,
225224
.done())
226225
.getBytes(StandardCharsets.UTF_8);
227226

228-
return new Page(YOUTUBEI_V1_URL + "next?key=" + getKey() + DISABLE_PRETTY_PRINT_PARAMETER,
229-
null, null, cookies, body);
227+
return new Page(YOUTUBEI_V1_URL + "next?" + DISABLE_PRETTY_PRINT_PARAMETER, null, null,
228+
cookies, body);
230229
}
231230

232231
@Override

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.DISABLE_PRETTY_PRINT_PARAMETER;
44
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
55
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getValidJsonResponseBody;
6+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getYoutubeMusicClientVersion;
67
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getYoutubeMusicHeaders;
78
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ALBUMS;
89
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_ARTISTS;
@@ -25,10 +26,8 @@
2526
import org.schabi.newpipe.extractor.downloader.Downloader;
2627
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
2728
import org.schabi.newpipe.extractor.exceptions.ParsingException;
28-
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
2929
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler;
3030
import org.schabi.newpipe.extractor.search.SearchExtractor;
31-
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
3231
import org.schabi.newpipe.extractor.utils.JsonUtils;
3332

3433
import java.io.IOException;
@@ -52,10 +51,8 @@ public YoutubeMusicSearchExtractor(final StreamingService service,
5251
@Override
5352
public void onFetchPage(@Nonnull final Downloader downloader)
5453
throws IOException, ExtractionException {
55-
final String[] youtubeMusicKeys = YoutubeParsingHelper.getYoutubeMusicKey();
56-
57-
final String url = "https://music.youtube.com/youtubei/v1/search?key="
58-
+ youtubeMusicKeys[0] + DISABLE_PRETTY_PRINT_PARAMETER;
54+
final String url = "https://music.youtube.com/youtubei/v1/search?"
55+
+ DISABLE_PRETTY_PRINT_PARAMETER;
5956

6057
final String params;
6158

@@ -86,7 +83,7 @@ public void onFetchPage(@Nonnull final Downloader downloader)
8683
.object("context")
8784
.object("client")
8885
.value("clientName", "WEB_REMIX")
89-
.value("clientVersion", youtubeMusicKeys[2])
86+
.value("clientVersion", getYoutubeMusicClientVersion())
9087
.value("hl", "en-GB")
9188
.value("gl", getExtractorContentCountry().getCountryCode())
9289
.value("platform", "DESKTOP")
@@ -206,15 +203,13 @@ public InfoItemsPage<InfoItem> getPage(final Page page)
206203

207204
final MultiInfoItemsCollector collector = new MultiInfoItemsCollector(getServiceId());
208205

209-
final String[] youtubeMusicKeys = YoutubeParsingHelper.getYoutubeMusicKey();
210-
211206
// @formatter:off
212207
final byte[] json = JsonWriter.string()
213208
.object()
214209
.object("context")
215210
.object("client")
216211
.value("clientName", "WEB_REMIX")
217-
.value("clientVersion", youtubeMusicKeys[2])
212+
.value("clientVersion", getYoutubeMusicClientVersion())
218213
.value("hl", "en-GB")
219214
.value("gl", getExtractorContentCountry().getCountryCode())
220215
.value("platform", "DESKTOP")
@@ -295,8 +290,7 @@ private void collectMusicStreamsFrom(final MultiInfoItemsCollector collector,
295290
}
296291

297292
@Nullable
298-
private Page getNextPageFrom(final JsonArray continuations)
299-
throws IOException, ParsingException, ReCaptchaException {
293+
private Page getNextPageFrom(final JsonArray continuations) {
300294
if (isNullOrEmpty(continuations)) {
301295
return null;
302296
}
@@ -306,7 +300,6 @@ private Page getNextPageFrom(final JsonArray continuations)
306300
final String continuation = nextContinuationData.getString("continuation");
307301

308302
return new Page("https://music.youtube.com/youtubei/v1/search?ctoken=" + continuation
309-
+ "&continuation=" + continuation + "&key="
310-
+ YoutubeParsingHelper.getYoutubeMusicKey()[0] + DISABLE_PRETTY_PRINT_PARAMETER);
303+
+ "&continuation=" + continuation + "&" + DISABLE_PRETTY_PRINT_PARAMETER);
311304
}
312305
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistExtractor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.YOUTUBEI_V1_URL;
55
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.extractPlaylistTypeFromPlaylistUrl;
66
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse;
7-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getKey;
87
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
98
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getImagesFromThumbnailsArray;
109
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
@@ -387,8 +386,7 @@ private Page getNextPageFrom(final JsonArray contents)
387386
.done())
388387
.getBytes(StandardCharsets.UTF_8);
389388

390-
return new Page(YOUTUBEI_V1_URL + "browse?key=" + getKey()
391-
+ DISABLE_PRETTY_PRINT_PARAMETER, body);
389+
return new Page(YOUTUBEI_V1_URL + "browse?" + DISABLE_PRETTY_PRINT_PARAMETER, body);
392390
} else {
393391
return null;
394392
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.DISABLE_PRETTY_PRINT_PARAMETER;
44
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.YOUTUBEI_V1_URL;
55
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse;
6-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getKey;
76
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
87
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
98
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.ALL;
@@ -247,8 +246,7 @@ private void collectStreamsFrom(final MultiInfoItemsCollector collector,
247246
}
248247

249248
@Nullable
250-
private Page getNextPageFrom(final JsonObject continuationItemRenderer) throws IOException,
251-
ExtractionException {
249+
private Page getNextPageFrom(final JsonObject continuationItemRenderer) {
252250
if (isNullOrEmpty(continuationItemRenderer)) {
253251
return null;
254252
}
@@ -257,8 +255,7 @@ private Page getNextPageFrom(final JsonObject continuationItemRenderer) throws I
257255
.getObject("continuationCommand")
258256
.getString("token");
259257

260-
final String url = YOUTUBEI_V1_URL + "search?key=" + getKey()
261-
+ DISABLE_PRETTY_PRINT_PARAMETER;
258+
final String url = YOUTUBEI_V1_URL + "search?" + DISABLE_PRETTY_PRINT_PARAMETER;
262259

263260
return new Page(url, token);
264261
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeDashManifestCreatorsTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.schabi.newpipe.extractor.services.youtube.dashmanifestcreators.YoutubeOtfDashManifestCreator;
99
import org.schabi.newpipe.extractor.services.youtube.dashmanifestcreators.YoutubeProgressiveDashManifestCreator;
1010
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
11-
import org.schabi.newpipe.extractor.stream.AudioTrackType;
1211
import org.schabi.newpipe.extractor.stream.DeliveryMethod;
1312
import org.schabi.newpipe.extractor.stream.Stream;
1413
import org.w3c.dom.Document;
@@ -89,7 +88,7 @@ class YoutubeDashManifestCreatorsTest {
8988

9089
@BeforeAll
9190
public static void setUp() throws Exception {
92-
YoutubeParsingHelper.resetClientVersionAndKey();
91+
YoutubeParsingHelper.resetClientVersion();
9392
YoutubeParsingHelper.setNumberGenerator(new Random(1));
9493
NewPipe.init(DownloaderTestImpl.getInstance());
9594

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeMixPlaylistExtractorTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
88
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.DISABLE_PRETTY_PRINT_PARAMETER;
99
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.YOUTUBEI_V1_URL;
10-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getKey;
1110
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
1211

1312
import com.grack.nanojson.JsonWriter;
@@ -91,7 +90,7 @@ void getPage() throws Exception {
9190
.getBytes(StandardCharsets.UTF_8);
9291

9392
final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page(
94-
YOUTUBEI_V1_URL + "next?key=" + getKey() + DISABLE_PRETTY_PRINT_PARAMETER,
93+
YOUTUBEI_V1_URL + "next?" + DISABLE_PRETTY_PRINT_PARAMETER,
9594
null, null, dummyCookie, body));
9695
assertFalse(streams.getItems().isEmpty());
9796
assertTrue(streams.hasNextPage());
@@ -180,7 +179,7 @@ void getPage() throws Exception {
180179
.getBytes(StandardCharsets.UTF_8);
181180

182181
final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page(
183-
YOUTUBEI_V1_URL + "next?key=" + getKey() + DISABLE_PRETTY_PRINT_PARAMETER,
182+
YOUTUBEI_V1_URL + "next?" + DISABLE_PRETTY_PRINT_PARAMETER,
184183
null, null, dummyCookie, body));
185184
assertFalse(streams.getItems().isEmpty());
186185
assertTrue(streams.hasNextPage());
@@ -268,7 +267,7 @@ void getPage() throws Exception {
268267
.getBytes(StandardCharsets.UTF_8);
269268

270269
final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page(
271-
YOUTUBEI_V1_URL + "next?key=" + getKey() + DISABLE_PRETTY_PRINT_PARAMETER,
270+
YOUTUBEI_V1_URL + "next?" + DISABLE_PRETTY_PRINT_PARAMETER,
272271
null, null, dummyCookie, body));
273272
assertFalse(streams.getItems().isEmpty());
274273
assertTrue(streams.hasNextPage());
@@ -386,7 +385,7 @@ void getPage() throws Exception {
386385
.getBytes(StandardCharsets.UTF_8);
387386

388387
final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page(
389-
YOUTUBEI_V1_URL + "next?key=" + getKey() + DISABLE_PRETTY_PRINT_PARAMETER,
388+
YOUTUBEI_V1_URL + "next?" + DISABLE_PRETTY_PRINT_PARAMETER,
390389
null, null, dummyCookie, body));
391390
assertFalse(streams.getItems().isEmpty());
392391
assertTrue(streams.hasNextPage());
@@ -453,7 +452,7 @@ void getPage() throws Exception {
453452
.getBytes(StandardCharsets.UTF_8);
454453

455454
final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page(
456-
YOUTUBEI_V1_URL + "next?key=" + getKey() + DISABLE_PRETTY_PRINT_PARAMETER,
455+
YOUTUBEI_V1_URL + "next?" + DISABLE_PRETTY_PRINT_PARAMETER,
457456
null, null, dummyCookie, body));
458457
assertFalse(streams.getItems().isEmpty());
459458
assertTrue(streams.hasNextPage());
@@ -542,7 +541,7 @@ void getPage() throws Exception {
542541
.getBytes(StandardCharsets.UTF_8);
543542

544543
final InfoItemsPage<StreamInfoItem> streams = extractor.getPage(new Page(
545-
YOUTUBEI_V1_URL + "next?key=" + getKey() + DISABLE_PRETTY_PRINT_PARAMETER,
544+
YOUTUBEI_V1_URL + "next?" + DISABLE_PRETTY_PRINT_PARAMETER,
546545
null, null, dummyCookie, body));
547546
assertFalse(streams.getItems().isEmpty());
548547
assertTrue(streams.hasNextPage());

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelperTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ public static void setUp() throws IOException {
2525
}
2626

2727
@Test
28-
void testAreHardcodedClientVersionAndKeyValid() throws IOException, ExtractionException {
29-
assertTrue(YoutubeParsingHelper.areHardcodedClientVersionAndKeyValid(),
30-
"Hardcoded client version and key are not valid anymore");
28+
void testIsHardcodedClientVersionValid() throws IOException, ExtractionException {
29+
assertTrue(YoutubeParsingHelper.isHardcodedClientVersionValid(),
30+
"Hardcoded client version is not valid anymore");
3131
}
3232

3333
@Test
34-
void testAreHardcodedYoutubeMusicKeysValid() throws IOException, ExtractionException {
35-
assertTrue(YoutubeParsingHelper.isHardcodedYoutubeMusicKeyValid(),
36-
"Hardcoded YouTube Music keys are not valid anymore");
34+
void testIsHardcodedYoutubeMusicClientVersionValid() throws IOException, ExtractionException {
35+
assertTrue(YoutubeParsingHelper.isHardcodedYoutubeMusicClientVersionValid(),
36+
"Hardcoded YouTube Music client version is not valid anymore");
3737
}
3838

3939
@Test

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTestsUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private YoutubeTestsUtils() {
2626
*/
2727
public static void ensureStateless() {
2828
YoutubeParsingHelper.setConsentAccepted(false);
29-
YoutubeParsingHelper.resetClientVersionAndKey();
29+
YoutubeParsingHelper.resetClientVersion();
3030
YoutubeParsingHelper.setNumberGenerator(new Random(1));
3131
YoutubeJavaScriptPlayerManager.clearAllCaches();
3232
}

0 commit comments

Comments
 (0)