Skip to content

Commit fcfb68f

Browse files
committed
[YouTube] Send Content-Type header in all POST requests
This header was not sent partially before and was added and guessed by OkHttp. This can create issues when using other HTTP clients than OkHttp, such as Cronet. Some code in the modified classes has been improved and / or deduplicated, and usages of the UTF_8 constant of the Utils class has been replaced by StandardCharsets.UTF_8 where possible. Note that this header has been not added in except in YoutubeDashManifestCreatorsUtils, as an empty body is sent in the POST requests made by this class.
1 parent ab1ed5a commit fcfb68f

7 files changed

Lines changed: 165 additions & 174 deletions

File tree

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -597,13 +597,14 @@ public static boolean areHardcodedClientVersionAndKeyValid()
597597
.end()
598598
.value("fetchLiveState", true)
599599
.end()
600-
.end().done().getBytes(UTF_8);
600+
.end().done().getBytes(StandardCharsets.UTF_8);
601601
// @formatter:on
602602

603603
final Map<String, List<String>> headers = new HashMap<>();
604604
headers.put("X-YouTube-Client-Name", singletonList("1"));
605605
headers.put("X-YouTube-Client-Version",
606606
singletonList(HARDCODED_CLIENT_VERSION));
607+
headers.put("Content-Type", Collections.singletonList("application/json"));
607608

608609
// This endpoint is fetched by the YouTube website to get the items of its main menu and is
609610
// pretty lightweight (around 30kB)
@@ -842,7 +843,7 @@ public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException,
842843
.end()
843844
.end()
844845
.value("input", "")
845-
.end().done().getBytes(UTF_8);
846+
.end().done().getBytes(StandardCharsets.UTF_8);
846847
// @formatter:on
847848

848849
final Map<String, List<String>> headers = new HashMap<>();
@@ -851,7 +852,7 @@ public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException,
851852
headers.put("X-YouTube-Client-Version", singletonList(
852853
HARDCODED_YOUTUBE_MUSIC_KEY[2]));
853854
headers.put("Origin", singletonList("https://music.youtube.com"));
854-
headers.put("Referer", singletonList("music.youtube.com"));
855+
headers.put("Referer", singletonList("https://music.youtube.com"));
855856
headers.put("Content-Type", singletonList("application/json"));
856857

857858
final Response response = getDownloader().post(url, headers, json);
@@ -1088,13 +1089,12 @@ public static JsonObject getJsonPostResponse(final String endpoint,
10881089
final Localization localization)
10891090
throws IOException, ExtractionException {
10901091
final Map<String, List<String>> headers = new HashMap<>();
1091-
addClientInfoHeaders(headers);
1092+
addYouTubeHeaders(headers);
10921093
headers.put("Content-Type", singletonList("application/json"));
10931094

1094-
final Response response = getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key="
1095-
+ getKey() + DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization);
1096-
1097-
return JsonUtils.toJsonObject(getValidJsonResponseBody(response));
1095+
return JsonUtils.toJsonObject(getValidJsonResponseBody(
1096+
getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key=" + getKey()
1097+
+ DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization)));
10981098
}
10991099

11001100
public static JsonObject getJsonAndroidPostResponse(
@@ -1123,17 +1123,18 @@ private static JsonObject getMobilePostResponse(
11231123
@Nonnull final String innerTubeApiKey,
11241124
@Nullable final String endPartOfUrlRequest) throws IOException, ExtractionException {
11251125
final Map<String, List<String>> headers = new HashMap<>();
1126-
headers.put("Content-Type", singletonList("application/json"));
11271126
headers.put("User-Agent", singletonList(userAgent));
11281127
headers.put("X-Goog-Api-Format-Version", singletonList("2"));
1128+
headers.put("Content-Type", singletonList("application/json"));
11291129

11301130
final String baseEndpointUrl = YOUTUBEI_V1_GAPIS_URL + endpoint + "?key=" + innerTubeApiKey
11311131
+ DISABLE_PRETTY_PRINT_PARAMETER;
11321132

1133-
final Response response = getDownloader().post(isNullOrEmpty(endPartOfUrlRequest)
1134-
? baseEndpointUrl : baseEndpointUrl + endPartOfUrlRequest,
1135-
headers, body, localization);
1136-
return JsonUtils.toJsonObject(getValidJsonResponseBody(response));
1133+
return JsonUtils.toJsonObject(getValidJsonResponseBody(
1134+
getDownloader().post(isNullOrEmpty(endPartOfUrlRequest)
1135+
? baseEndpointUrl
1136+
: baseEndpointUrl + endPartOfUrlRequest,
1137+
headers, body, localization)));
11371138
}
11381139

11391140
@Nonnull
@@ -1336,6 +1337,17 @@ public static String getIosUserAgent(@Nullable final Localization localization)
13361337
+ ")";
13371338
}
13381339

1340+
@Nonnull
1341+
public static Map<String, List<String>> getYoutubeMusicHeaders() {
1342+
final Map<String, List<String>> headers = new HashMap<>();
1343+
headers.put("X-YouTube-Client-Name", Collections.singletonList(youtubeMusicKey[1]));
1344+
headers.put("X-YouTube-Client-Version", Collections.singletonList(youtubeMusicKey[2]));
1345+
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
1346+
headers.put("Referer", Collections.singletonList("https://music.youtube.com"));
1347+
headers.put("Content-Type", Collections.singletonList("application/json"));
1348+
return headers;
1349+
}
1350+
13391351
/**
13401352
* Add required headers and cookies to an existing headers Map.
13411353
* @see #addClientInfoHeaders(Map)

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/dashmanifestcreators/YoutubeDashManifestCreatorsUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.util.Map;
3636
import java.util.Objects;
3737

38-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.addClientInfoHeaders;
3938
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getAndroidUserAgent;
4039
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getIosUserAgent;
4140
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isAndroidStreamingUrl;
@@ -707,7 +706,8 @@ private static Response getStreamingWebUrlWithoutRedirects(
707706
throws CreationException {
708707
try {
709708
final Map<String, List<String>> headers = new HashMap<>();
710-
addClientInfoHeaders(headers);
709+
headers.put("Origin", Collections.singletonList("https://www.youtube.com"));
710+
headers.put("Referer", Collections.singletonList("https://www.youtube.com"));
711711

712712
String responseMimeType = "";
713713

0 commit comments

Comments
 (0)