Skip to content

Commit fc036e0

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 d2316e6 commit fc036e0

7 files changed

Lines changed: 182 additions & 180 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
@@ -592,13 +592,14 @@ public static boolean areHardcodedClientVersionAndKeyValid()
592592
.end()
593593
.value("fetchLiveState", true)
594594
.end()
595-
.end().done().getBytes(UTF_8);
595+
.end().done().getBytes(StandardCharsets.UTF_8);
596596
// @formatter:on
597597

598598
final Map<String, List<String>> headers = new HashMap<>();
599599
headers.put("X-YouTube-Client-Name", singletonList("1"));
600600
headers.put("X-YouTube-Client-Version",
601601
singletonList(HARDCODED_CLIENT_VERSION));
602+
headers.put("Content-Type", Collections.singletonList("application/json"));
602603

603604
// This endpoint is fetched by the YouTube website to get the items of its main menu and is
604605
// pretty lightweight (around 30kB)
@@ -816,7 +817,7 @@ public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException,
816817
.end()
817818
.end()
818819
.value("input", "")
819-
.end().done().getBytes(UTF_8);
820+
.end().done().getBytes(StandardCharsets.UTF_8);
820821
// @formatter:on
821822

822823
final Map<String, List<String>> headers = new HashMap<>();
@@ -825,7 +826,7 @@ public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException,
825826
headers.put("X-YouTube-Client-Version", singletonList(
826827
HARDCODED_YOUTUBE_MUSIC_KEY[2]));
827828
headers.put("Origin", singletonList("https://music.youtube.com"));
828-
headers.put("Referer", singletonList("music.youtube.com"));
829+
headers.put("Referer", singletonList("https://music.youtube.com"));
829830
headers.put("Content-Type", singletonList("application/json"));
830831

831832
final Response response = getDownloader().post(url, headers, json);
@@ -1062,13 +1063,12 @@ public static JsonObject getJsonPostResponse(final String endpoint,
10621063
final Localization localization)
10631064
throws IOException, ExtractionException {
10641065
final Map<String, List<String>> headers = new HashMap<>();
1065-
addClientInfoHeaders(headers);
1066+
addYouTubeHeaders(headers);
10661067
headers.put("Content-Type", singletonList("application/json"));
10671068

1068-
final Response response = getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key="
1069-
+ getKey() + DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization);
1070-
1071-
return JsonUtils.toJsonObject(getValidJsonResponseBody(response));
1069+
return JsonUtils.toJsonObject(getValidJsonResponseBody(
1070+
getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key=" + getKey()
1071+
+ DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization)));
10721072
}
10731073

10741074
public static JsonObject getJsonAndroidPostResponse(
@@ -1097,17 +1097,18 @@ private static JsonObject getMobilePostResponse(
10971097
@Nonnull final String innerTubeApiKey,
10981098
@Nullable final String endPartOfUrlRequest) throws IOException, ExtractionException {
10991099
final Map<String, List<String>> headers = new HashMap<>();
1100-
headers.put("Content-Type", singletonList("application/json"));
11011100
headers.put("User-Agent", singletonList(userAgent));
11021101
headers.put("X-Goog-Api-Format-Version", singletonList("2"));
1102+
headers.put("Content-Type", singletonList("application/json"));
11031103

11041104
final String baseEndpointUrl = YOUTUBEI_V1_GAPIS_URL + endpoint + "?key=" + innerTubeApiKey
11051105
+ DISABLE_PRETTY_PRINT_PARAMETER;
11061106

1107-
final Response response = getDownloader().post(isNullOrEmpty(endPartOfUrlRequest)
1108-
? baseEndpointUrl : baseEndpointUrl + endPartOfUrlRequest,
1109-
headers, body, localization);
1110-
return JsonUtils.toJsonObject(getValidJsonResponseBody(response));
1107+
return JsonUtils.toJsonObject(getValidJsonResponseBody(
1108+
getDownloader().post(isNullOrEmpty(endPartOfUrlRequest)
1109+
? baseEndpointUrl
1110+
: baseEndpointUrl + endPartOfUrlRequest,
1111+
headers, body, localization)));
11111112
}
11121113

11131114
@Nonnull
@@ -1285,6 +1286,17 @@ public static String getIosUserAgent(@Nullable final Localization localization)
12851286
+ ")";
12861287
}
12871288

1289+
@Nonnull
1290+
public static Map<String, List<String>> getYoutubeMusicHeaders() {
1291+
final Map<String, List<String>> headers = new HashMap<>();
1292+
headers.put("X-YouTube-Client-Name", Collections.singletonList(youtubeMusicKey[1]));
1293+
headers.put("X-YouTube-Client-Version", Collections.singletonList(youtubeMusicKey[2]));
1294+
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
1295+
headers.put("Referer", Collections.singletonList("https://music.youtube.com"));
1296+
headers.put("Content-Type", Collections.singletonList("application/json"));
1297+
return headers;
1298+
}
1299+
12881300
/**
12891301
* Add required headers and cookies to an existing headers Map.
12901302
* @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)