Skip to content

Commit 6017730

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 509f42d commit 6017730

File tree

7 files changed

+186
-184
lines changed

7 files changed

+186
-184
lines changed

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -591,13 +591,14 @@ public static boolean areHardcodedClientVersionAndKeyValid()
591591
.end()
592592
.value("fetchLiveState", true)
593593
.end()
594-
.end().done().getBytes(UTF_8);
594+
.end().done().getBytes(StandardCharsets.UTF_8);
595595
// @formatter:on
596596

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

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

823824
final Map<String, List<String>> headers = new HashMap<>();
824-
headers.put("X-YouTube-Client-Name", Collections.singletonList(
825-
HARDCODED_YOUTUBE_MUSIC_KEY[1]));
826-
headers.put("X-YouTube-Client-Version", Collections.singletonList(
827-
HARDCODED_YOUTUBE_MUSIC_KEY[2]));
825+
headers.put("X-YouTube-Client-Name",
826+
Collections.singletonList(HARDCODED_YOUTUBE_MUSIC_KEY[1]));
827+
headers.put("X-YouTube-Client-Version",
828+
Collections.singletonList(HARDCODED_YOUTUBE_MUSIC_KEY[2]));
828829
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
829-
headers.put("Referer", Collections.singletonList("music.youtube.com"));
830+
headers.put("Referer", Collections.singletonList("https://music.youtube.com"));
830831
headers.put("Content-Type", Collections.singletonList("application/json"));
831832

832833
final Response response = getDownloader().post(url, headers, json);
@@ -1065,13 +1066,12 @@ public static JsonObject getJsonPostResponse(final String endpoint,
10651066
final Localization localization)
10661067
throws IOException, ExtractionException {
10671068
final Map<String, List<String>> headers = new HashMap<>();
1068-
addClientInfoHeaders(headers);
1069+
addYouTubeHeaders(headers);
10691070
headers.put("Content-Type", Collections.singletonList("application/json"));
10701071

1071-
final Response response = getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key="
1072-
+ getKey() + DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization);
1073-
1074-
return JsonUtils.toJsonObject(getValidJsonResponseBody(response));
1072+
return JsonUtils.toJsonObject(getValidJsonResponseBody(
1073+
getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key=" + getKey()
1074+
+ DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization)));
10751075
}
10761076

10771077
public static JsonObject getJsonAndroidPostResponse(
@@ -1100,17 +1100,18 @@ private static JsonObject getMobilePostResponse(
11001100
@Nonnull final String innerTubeApiKey,
11011101
@Nullable final String endPartOfUrlRequest) throws IOException, ExtractionException {
11021102
final Map<String, List<String>> headers = new HashMap<>();
1103-
headers.put("Content-Type", Collections.singletonList("application/json"));
11041103
headers.put("User-Agent", Collections.singletonList(userAgent));
11051104
headers.put("X-Goog-Api-Format-Version", Collections.singletonList("2"));
1105+
headers.put("Content-Type", Collections.singletonList("application/json"));
11061106

11071107
final String baseEndpointUrl = YOUTUBEI_V1_GAPIS_URL + endpoint + "?key=" + innerTubeApiKey
11081108
+ DISABLE_PRETTY_PRINT_PARAMETER;
11091109

1110-
final Response response = getDownloader().post(isNullOrEmpty(endPartOfUrlRequest)
1111-
? baseEndpointUrl : baseEndpointUrl + endPartOfUrlRequest,
1112-
headers, body, localization);
1113-
return JsonUtils.toJsonObject(getValidJsonResponseBody(response));
1110+
return JsonUtils.toJsonObject(getValidJsonResponseBody(
1111+
getDownloader().post(isNullOrEmpty(endPartOfUrlRequest)
1112+
? baseEndpointUrl
1113+
: baseEndpointUrl + endPartOfUrlRequest,
1114+
headers, body, localization)));
11141115
}
11151116

11161117
@Nonnull
@@ -1288,6 +1289,17 @@ public static String getIosUserAgent(@Nullable final Localization localization)
12881289
+ ")";
12891290
}
12901291

1292+
@Nonnull
1293+
public static Map<String, List<String>> getYoutubeMusicHeaders() {
1294+
final Map<String, List<String>> headers = new HashMap<>();
1295+
headers.put("X-YouTube-Client-Name", Collections.singletonList(youtubeMusicKey[1]));
1296+
headers.put("X-YouTube-Client-Version", Collections.singletonList(youtubeMusicKey[2]));
1297+
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
1298+
headers.put("Referer", Collections.singletonList("https://music.youtube.com"));
1299+
headers.put("Content-Type", Collections.singletonList("application/json"));
1300+
return headers;
1301+
}
1302+
12911303
/**
12921304
* Add required headers and cookies to an existing headers Map.
12931305
* @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;
@@ -708,7 +707,8 @@ private static Response getStreamingWebUrlWithoutRedirects(
708707
throws CreationException {
709708
try {
710709
final Map<String, List<String>> headers = new HashMap<>();
711-
addClientInfoHeaders(headers);
710+
headers.put("Origin", Collections.singletonList("https://www.youtube.com"));
711+
headers.put("Referer", Collections.singletonList("https://www.youtube.com"));
712712

713713
String responseMimeType = "";
714714

0 commit comments

Comments
 (0)