4040import java .time .ZoneOffset ;
4141import java .time .format .DateTimeParseException ;
4242import java .util .ArrayList ;
43+ import java .util .Arrays ;
4344import java .util .Collections ;
4445import java .util .HashMap ;
4546import java .util .List ;
@@ -78,15 +79,15 @@ private YoutubeParsingHelper() {
7879
7980 public static final String YOUTUBEI_V1_URL = "https://www.youtube.com/youtubei/v1/" ;
8081
81- private static final String HARDCODED_CLIENT_VERSION = "2.20210728 .00.00" ;
82+ private static final String HARDCODED_CLIENT_VERSION = "2.20220107 .00.00" ;
8283 private static final String HARDCODED_KEY = "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8" ;
8384 private static final String MOBILE_YOUTUBE_KEY = "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w" ;
84- private static final String MOBILE_YOUTUBE_CLIENT_VERSION = "16.29.38 " ;
85+ private static final String MOBILE_YOUTUBE_CLIENT_VERSION = "16.49.37 " ;
8586 private static String clientVersion ;
8687 private static String key ;
8788
8889 private static final String [] HARDCODED_YOUTUBE_MUSIC_KEY =
89- {"AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30" , "67" , "1.20210726 .00.01 " };
90+ {"AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30" , "67" , "1.20220103 .00.00 " };
9091 private static String [] youtubeMusicKey ;
9192
9293 private static boolean keyAndVersionExtracted = false ;
@@ -551,40 +552,53 @@ private static void extractClientVersionAndKey() throws IOException, ExtractionE
551552 }
552553
553554 /**
554- * Get the client version
555+ * Get the client version used by YouTube website on InnerTube requests.
555556 */
556557 public static String getClientVersion () throws IOException , ExtractionException {
557558 if (!isNullOrEmpty (clientVersion )) {
558559 return clientVersion ;
559560 }
560- if (areHardcodedClientVersionAndKeyValid ()) {
561- clientVersion = HARDCODED_CLIENT_VERSION ;
562- return clientVersion ;
563- }
564561
565562 extractClientVersionAndKey ();
566- return clientVersion ;
563+
564+ if (keyAndVersionExtracted ) {
565+ return clientVersion ;
566+ } else {
567+ if (areHardcodedClientVersionAndKeyValid ()) {
568+ clientVersion = HARDCODED_CLIENT_VERSION ;
569+ return clientVersion ;
570+ }
571+ }
572+ throw new ExtractionException ("Could not get YouTube WEB client version" );
567573 }
568574
569575 /**
570- * Get the key
576+ * Get the internal API key used by YouTube website on InnerTube requests.
571577 */
572578 public static String getKey () throws IOException , ExtractionException {
573579 if (!isNullOrEmpty (key )) {
574580 return key ;
575581 }
576- if (areHardcodedClientVersionAndKeyValid ()) {
577- key = HARDCODED_KEY ;
582+
583+ extractClientVersionAndKey ();
584+
585+ if (keyAndVersionExtracted ) {
578586 return key ;
587+ } else {
588+ if (areHardcodedClientVersionAndKeyValid ()) {
589+ key = HARDCODED_KEY ;
590+ return key ;
591+ }
579592 }
580593
581- extractClientVersionAndKey ();
582- return key ;
594+ // The ANDROID API key is also valid with the WEB client so return it if we couldn't
595+ // extract the WEB API key.
596+ return MOBILE_YOUTUBE_KEY ;
583597 }
584598
585599 /**
586600 * <p>
587- * <b>Only use in tests.</b>
601+ * <b>Only used in tests.</b>
588602 * </p>
589603 *
590604 * <p>
@@ -600,11 +614,12 @@ public static String getKey() throws IOException, ExtractionException {
600614 public static void resetClientVersionAndKey () {
601615 clientVersion = null ;
602616 key = null ;
617+ keyAndVersionExtracted = false ;
603618 }
604619
605620 /**
606621 * <p>
607- * <b>Only use in tests.</b>
622+ * <b>Only used in tests.</b>
608623 * </p>
609624 */
610625 public static void setNumberGenerator (final Random random ) {
@@ -1128,7 +1143,7 @@ public static void addClientInfoHeaders(@Nonnull final Map<String, List<String>>
11281143 */
11291144 public static void addCookieHeader (@ Nonnull final Map <String , List <String >> headers ) {
11301145 if (headers .get ("Cookie" ) == null ) {
1131- headers .put ("Cookie" , Collections . singletonList (generateConsentCookie ()));
1146+ headers .put ("Cookie" , Arrays . asList (generateConsentCookie ()));
11321147 } else {
11331148 headers .get ("Cookie" ).add (generateConsentCookie ());
11341149 }
0 commit comments