LuanRT / YouTube.js

A wrapper around YouTube's internal API — reverse engineering InnerTube
https://www.npmjs.com/package/youtubei.js
MIT License
3.32k stars 205 forks source link

InnertubeError: Request to failed with status 400 ⚠️ #605

Closed byadems closed 4 months ago

byadems commented 4 months ago

Steps to reproduce

dlsong will be enough to encounter this error.

Failure Logs

InnertubeError: Request to https://www.youtube.com/youtubei/vtrue/search?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8&prettyPrint=false&alt=json failed with status 400

at HTTPClient.<anonymous> (/railway/Raganork/node_modules/youtubei.js/dist/src/utils/HTTPClient.js:113:19)

at Generator.next (<anonymous>)

at fulfilled (/railway/Raganork/node_modules/youtubei.js/dist/src/utils/HTTPClient.js:5:58)

at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {

info: `<html lang="en" dir="ltr"><head><title>Oops</title><style nonce="2TPbi3428KDtEFdWvFTTpA">html{font-family:Roboto,Arial,sans-serif;font-size:14px}body{background-color:#f9f9f9;margin:0}#content{max-width:440px;margin:128px auto}svg{display:block;pointer-events:none}#monkey{width:280px;margin:0 auto}h1,p{text-align:center;margin:0;color:#131313}h1{padding:24px 0 8px;font-size:24px;font-weight:400}p{line-height:21px}sentinel{}</style><link rel="shortcut icon" href="https://www.youtube.com/img/favicon.ico"; type="image/x-icon"><link rel="icon" href="https://www.youtube.com/img/favicon_32.png"; sizes="32x32"><link rel="icon" href="https://www.youtube.com/img/favicon_48.png"; sizes="48x48"><link rel="icon" href="https://www.youtube.com/img/favicon_96.png"; sizes="96x96"><link rel="icon" href="https://www.youtube.com/img/favicon_144.png"; sizes="144x144"></head><body><div id="content"><h1>Something went wrong</h1><p><svg id="monkey" viewBox="0 0 490 525"><path fill="#6A1B9A" d="M325 85c1 12-1 25-5 38-8 29-31 52-60 61-26 8-54 14-81 18-37 6-26-37-38-72l-4-4c0-17-9-33 4-37l33-4c9-2 9-21 11-30 1-7 3-14 5-21 8-28 40-42 68-29 18 9 36 19 50 32 13 11 16 31 17 48z"/><path fill="none" stroke="#6A1B9A" stroke-width="24" stroke-linecap="round" stroke-miterlimit="10" d="M431 232c3 15 21 19 34 11 15-9 14-30 5-43-12-17-38-25-59-10-23 18-27 53-21 97s1 92-63 108"/><path fill="#6A1B9A" d="M284 158c35 40 63 85 86 133 24 52-6 113-62 123-2 0-4 1-6 1-53 9-101-33-101-87V188l83-30z"/><path fill="#F7CB4D" d="M95 152c-3-24 13-57 46-64l27-5c9-2 16-19 17-28l3-15 20-3c44 14 42 55 18 69 22 0 39 26 32 53-5 18-20 32-39 36-13 3-26 5-40 8-50 8-80-14-84-51z"/><path fill="#6A1B9A" d="M367 392c-21 18-77 70-25 119h-61c-27-29-32-69 1-111l85-8z"/><path fill="#6A1B9A" d="M289 399c-21 18-84 62-32 111h-61c-37-34-5-104 43-134l50 23z"/><path fill="#EDB526" d="M185 56l3-15 20-3c25 8 35 25 35 41-12-18-49-29-58-23z"/><path fill="#E62117" d="M190 34c8-28 40-42 68-29 18 9 36 19 50 32 10 9 14 23 16 37L187 46l3-12z"/><path fill="#8E24AA" d="M292 168c0 0 0 201 0 241s20 98 91 85l-16-54c-22 12-31-17-31-37 0-20 0-108 0-137S325 200 292 168z"/><path fill="#F7CB4D" d="M284 79c11-9 23-17 35-23 25-12 54 7 59 38v1c4 27-13 51-36 53-12 1-25 1-37 0-22-1-39-27-32-52v-1c2-6 6-12 11-16z"/><path fill="#6A1B9A" d="M201 203s0 84-95 140l22 42s67-25 89-86-16-96-16-96z"/><path fill="#BE2117" d="M224 54l-67-14c-10-2-13-15-5-21s18-6 26 0l46 35z"/><circle fill="#4A148C" cx="129" cy="161" r="12"/><circle fill="#4A148C" cx="212" cy="83" r="7"/><circle fill="#4A148C" cx="189" cy="79" r="7"/><path fill="#F7CB4D" d="M383 493c11-3 19-8 25-13 7-10 4-16-5-20 8-9 2-22-8-18 1-1 1-2 1-3 3-9-9-15-15-8-3 4-8 7-13 9l15 53z"/><path fill="#EDB526" d="M252 510c5 6 0 15-9 15h-87c-10 0-16-8-13-15 5-12 21-19 36-16l73 16z"/><ellipse transform="rotate(19.126 278.35 14.787)" fill="#E62117" cx="278" cy="15" rx="9" ry="7"/><path fill="#F7CB4D" d="M341 510c5 6 0 15-9 15h-87c-10 0-16-8-13-15 5-12 21-19 36-16l73 16z"/><path fill="#EDB526" d="M357 90c-12-19-35-23-55-11-19 12-25 32-13 52"/><path fill="#E62117" d="M110 427l21-9c5-2 7-8 5-13l-42-94c-3-6-9-9-15-6l-11 5c-6 2-9 9-7 15l36 97c2 5 8 7 13 5z"/><path fill="#B0BEC5" d="M37 278l41-17c11-4 22-5 33-1 5 2 10 4 14 6 6 3 4 11-3 11-9 0-18 1-26 3l2 12c1 6-2 11-8 13l-36 15c-5 2-10 1-14-2l-9-7-2 17c0 2-2 4-4 5l-3 1c-3 1-7 0-8-3L1 300c-1-3 0-7 4-9l4-2c2-1 5 0 7 1l12 10 1-11c0-5 3-9 8-11z"/><path fill="#F7CB4D" d="M103 373c10 2 14 10 8 19 6-1 10 4 10 9 0 3-3 6-6 7l-26 11c-2 1-5 1-8 0-6-3-7-9-2-16-7-1-13-9-6-17-8-1-12-8-8-15l3-3 23-11c9-4 19 8 12 16z"/><ellipse transform="rotate(173.3 233.455 334.51)" fill="#8E24AA" cx="234" cy="335" rx="32" ry="46"/></svg></p><style nonce="2TPbi3428KDtEFdWvFTTpA">#yt-masthead{margin:15px auto;width:440px;margin-top:25px}#logo-container{margin-right:5px;float:left;cursor:pointer;text-decoration:none}.logo{background:no-repeat url(//www.gstatic.com/youtube/img/branding/youtubelogo/1x/youtubelogo_30.png);width:125px;height:30px;cursor:pointer;display:inline-block}#masthead-search{display:-webkit-box;display:-webkit-flex;display:flex;margin-top:3px;max-width:650px;overflow:hidden;padding:0;position:relative}.search-button{border-left:0;border-top-left-radius:0;border-bottom-left-radius:0;float:right;height:29px;padding:0;border:solid 1px transparent;border-color:#d3d3d3;background:#f8f8f8;color:#333;cursor:pointer}.search-button:hover{border-color:#c6c6c6;background:#f0f0f0;-webkit-box-shadow:0 1px 0 rgba(0,0,0,.1);box-shadow:0 1px 0 rgba(0,0,0,.1)}.search-button-content{border:none;display:block;opacity:.6;padding:0;text-indent:-10000px;background:no-repeat url(//www.gstatic.com/youtube/src/web/htdocs/img/search.png);-webkit-background-size:auto auto;background-size:auto;width:15px;height:15px;-webkit-box-shadow:none;box-shadow:none;margin:0 25px}#masthead-search-terms-border{-webkit-box-flex:1;-webkit-flex:1 1 auto;flex:1 1 auto;border:1px solid #ccc;-webkit-box-shadow:inset 0 1px 2px #eee;box-shadow:inset 0 1px 2px #eee;background-color:#fff;font-size:14px;height:29px;line-height:30px;margin:0 0 2px;overflow:hidden;position:relative;-webkit-box-sizing:border-box;box-sizing:border-box;-webkit-transition:border-color .2s ease;transition:border-color .2s ease}#masthead-search-terms{background:transparent;border:0;font-size:16px;height:100%;left:0;margin:0;outline:none;padding:2px 6px;position:absolute;width:100%;-webkit-box-sizing:border-box;box-sizing:border-box}sentinel{}</style><div id="yt-masthead"><a id="logo-container" href="https://www.youtube.com/"; title="YouTube home"><span class="logo" title="YouTube home"></span></a><form id="masthead-search" class="search-form" action="https://www.youtube.com/results"><script nonce="74aoomG7ep2X61_RLtnmhg">document.addEventListener('DOMContentLoaded', function () {document.getElementById('masthead-search').addEventListener('submit', function(e) {if (document.getElementById('masthead-search-terms').value == '') {e.preventDefault();}});});</script><div id="masthead-search-terms-border" dir="ltr"><input id="masthead-search-terms" autocomplete="off" name="search_query" value="" type="text" placeholder="Search" title="Search" aria-label="Search"><script nonce="74aoomG7ep2X61_RLtnmhg">document.addEventListener('DOMContentLoaded', function () {document.getElementById('masthead-search-terms').addEventListener('keydown', function() {if (!this.value && (event.keyCode == 40 || event.keyCode == 32 || event.keyCode == 34)) {this.onkeydown = null; this.blur();}});});</script></div><button id="masthead-search-button" class="search-button" type="submit" dir="ltr"><script nonce="74aoomG7ep2X61_RLtnmhg">document.addEventListener('DOMContentLoaded', function () {document.getElementById('masthead-search-button').addEventListener('click', function(e) {if (document.getElementById('masthead-search-terms').value == '') {e.preventDefault(); return;}e.preventDefault(); document.getElementById('masthead-search').submit();});});</script><span class="search-button-content">Search</span></button></form></div></div></body></html>`,

Expected behavior

Successfully download a video or song file

Current behavior

He constantly makes mistakes

Version

Default

Anything else?

No response

Checklist

byadems commented 4 months ago

it's been like this for 2 days and even though it's been corrected, the result is the same.

LuanRT commented 4 months ago

Please update to v9.1.0.

byadems commented 4 months ago

Please update to v9.1.0.

So how can I make the Update?

sgebr01 commented 4 months ago

@byadems

I was having the same issue up until now, I believe he just means to update the package through npm. This did fix the issue for me.

npm install youtubei.js@latest
byadems commented 4 months ago

@byadems

I was having the same issue up until now, I believe he just means to update the package through npm. This did fix the issue for me.

npm install youtubei.js@latest

Do you know how to update on the Railway platform?

LuanRT commented 4 months ago

@byadems Are you using Node.js or is it something else? Deleting your node_modules folder and then running npm install should work.

byadems commented 4 months ago

@byadems

@LuanRT Node via Railway.i'm running it through js. The name of the related Bot is also called Raganork-md, I asked the developer to fix it, he said he couldn't be interested right now. Can't I fix it myself, please help me.

Azarattum commented 4 months ago

@LuanRT, I'm also having 400 error code running with 9.1.0 version.

This code gives me an error:

import { Innertube } from "youtubei.js";

(async () => {
  const yt = await Innertube.create();
  yt.music.search("hello", { type: "song" }).then(console.log);
})();

Here is a full error:

file:///home/tmp/node_modules/.pnpm/youtubei.js@9.1.0/node_modules/youtubei.js/dist/src/utils/HTTPClient.js:99
            throw new InnertubeError(`Request to ${response.url} failed with status ${response.status}`, yield response.text());
                  ^

InnertubeError: Request to https://www.youtube.com/youtubei/v1/search?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8&prettyPrint=false&alt=json failed with status 400
    at HTTPClient.<anonymous> (file:///home/tmp/node_modules/.pnpm/youtubei.js@9.1.0/node_modules/youtubei.js/dist/src/utils/HTTPClient.js:99:19)
    at Generator.next (<anonymous>)
    at fulfilled (/home/tmp/node_modules/.pnpm/tslib@2.6.2/node_modules/tslib/tslib.js:166:62)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  info: '{\n' +
    '  "error": {\n' +
    '    "code": 400,\n' +
    '    "message": "Request contains an invalid argument.",\n' +
    '    "errors": [\n' +
    '      {\n' +
    '        "message": "Request contains an invalid argument.",\n' +
    '        "domain": "global",\n' +
    '        "reason": "badRequest"\n' +
    '      }\n' +
    '    ],\n' +
    '    "status": "INVALID_ARGUMENT"\n' +
    '  }\n' +
    '}\n',
  date: 2024-03-14T16:08:44.681Z,
  version: '9.1.0'
}

Node.js v21.5.0

Surprisingly, running the same code from my local machine gives an expected result. Can it be that youtube blocked my somehow? How do I find out? Is it permanent? And why the error code is so weird?..

Azarattum commented 4 months ago

Here is the test suit running on maching where the error ocuured:

 FAIL   node  test/main.test.ts (31.697 s)
  YouTube.js Tests
    Main
      ✓ Innertube#getInfo (638 ms)
      ✓ Innertube#getBasicInfo (135 ms)
      ✓ Innertube#getShortsWatchItem (685 ms)
      ✓ Innertube#getShortsWatchItem#Continue (760 ms)
      ✓ Innertube#search (806 ms)
      ✓ Innertube#getSearchSuggestions (114 ms)
      ✓ Innertube#getGuide (40 ms)
      ✓ Innertube#getTrending (346 ms)
      ✓ Innertube#getPlaylist (91 ms)
      ✓ Innertube#resolveURL (32 ms)
      ✓ Innertube#download (2958 ms)
      Innertube#getBasicInfo
        ✓ Format#language multiple audio tracks (176 ms)
        ✓ Format#language single audio track with captions (193 ms)
      Innertube#search
        ✓ Search#getContinuation (792 ms)
      Innertube#getComments
        ✓ Comments#getContinuation (554 ms)
        CommentThread#getReplies
          ✓ CommentThread#getContinuation (171 ms)
      Innertube#getHomeFeed
        ✕ HomeFeed#getContinuation (2 ms)
      Innertube#getChannel
        ✓ Channel#getHome (2 ms)
        ✓ Channel#getPlaylists (90 ms)
        ✓ Channel#getCommunity (166 ms)
        ✓ Channel#getAbout (53 ms)
        ✓ Channel#search (454 ms)
        Channel#getVideos
          ✓ Channel#getContinuation (205 ms)
          ✓ Channel#applyFilter (172 ms)
      Innertube#getHashtag
        ✓ HashtagFeed#getContinuation (553 ms)
    YouTube Music
      ✓ Innertube#music.getInfo (133 ms)
      ✕ Innertube#music.getExplore (40 ms)
      ✓ Innertube#music.getArtist (123 ms)
      ✕ Innertube#music.getAlbum (56 ms)
      ✓ Innertube#music.getPlaylist (131 ms)
      ✕ Innertube#music.getLyrics (191 ms)
      ✕ Innertube#music.getUpNext (494 ms)
      ✕ Innertube#music.getRelated (232 ms)
      ✓ Innertube#music.getSearchSuggestions (66 ms)
      Innertube#music.search
        ✓ Search#applyFilter (302 ms)
        ✕ Search#getContinuation (1 ms)
      Innertube#music.getHomeFeed
        ✕ HomeFeed#getContinuation
        ✕ HomeFeed#applyFilter
    YouTube Kids
      ✓ Innertube#kids.getInfo (691 ms)
      ✓ Innertube#kids.getHomeFeed (443 ms)
      ✓ Innertube#kids.getChannel (167 ms)
      ✓ Innertube#kids.search (500 ms)

  ● YouTube.js Tests › Main › Innertube#getHomeFeed › HomeFeed#getContinuation

    There are no continuations.

      200 |     if (this.#continuation) {
      201 |       if (this.#continuation.length === 0)
    > 202 |         throw new InnertubeError('There are no continuations.');
          |               ^
      203 |
      204 |       const response = await this.#continuation[0].endpoint.call<T>(this.#actions, { parse: true });
      205 |

      at HomeFeed.<anonymous> (src/core/mixins/Feed.ts:202:15)
      at node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:167:75
      at __awaiter (node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:163:16)
      at HomeFeed.getContinuationData (bundle/node.cjs:13137:41)
      at HomeFeed.<anonymous> (src/core/mixins/Feed.ts:212:19)
      at node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:167:75

  ● YouTube.js Tests › YouTube Music › Innertube#music.search › Search#getContinuation

    Continuation not found.

      72 |   async getContinuation(): Promise<SearchContinuation> {
      73 |     if (!this.#continuation)
    > 74 |       throw new InnertubeError('Continuation not found.');
         |             ^
      75 |
      76 |     const response = await this.#actions.execute('/search', {
      77 |       continuation: this.#continuation,

      at Search2.<anonymous> (src/parser/ytmusic/Search.ts:74:13)
      at node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:167:75
      at __awaiter (node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:163:16)
      at Search2.getContinuation (bundle/node.cjs:15423:41)
      at test/main.test.ts:314:55
      at node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:167:75

  ● YouTube.js Tests › YouTube Music › Innertube#music.getHomeFeed › HomeFeed#getContinuation

    Expected node of any type MusicCarouselShelf, MusicTasteBuilderShelf, got ItemSection

      463 |             if (node.is(...types))
      464 |               return node;
    > 465 |             throw new ParsingError(`Expected node of any type ${types.map((type) => type.type).join(', ')}, got ${(node as YTNode).type}`);
          |                   ^
      466 |           }));
      467 |         };
      468 |       }

      at src/parser/helpers.ts:465:19
          at Array.map (<anonymous>)
      at Proxy.<anonymous> (src/parser/helpers.ts:462:33)
      at new HomeFeed (src/parser/ytmusic/HomeFeed.ts:43:59)
      at Music.<anonymous> (src/core/clients/Music.ts:137:12)
      at fulfilled (node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:164:62)

  ● YouTube.js Tests › YouTube Music › Innertube#music.getHomeFeed › HomeFeed#applyFilter

    Expected node of any type MusicCarouselShelf, MusicTasteBuilderShelf, got ItemSection

      463 |             if (node.is(...types))
      464 |               return node;
    > 465 |             throw new ParsingError(`Expected node of any type ${types.map((type) => type.type).join(', ')}, got ${(node as YTNode).type}`);
          |                   ^
      466 |           }));
      467 |         };
      468 |       }

      at src/parser/helpers.ts:465:19
          at Array.map (<anonymous>)
      at Proxy.<anonymous> (src/parser/helpers.ts:462:33)
      at new HomeFeed (src/parser/ytmusic/HomeFeed.ts:43:59)
      at Music.<anonymous> (src/core/clients/Music.ts:137:12)
      at fulfilled (node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:164:62)

  ● YouTube.js Tests › YouTube Music › Innertube#music.getExplore

    expect(received).toBeGreaterThan(expected)

    Expected: > 0
    Received:   0

      349 |       expect(explore).toBeDefined();
      350 |       expect(explore.sections).toBeDefined();
    > 351 |       expect(explore.sections?.length).toBeGreaterThan(0);
          |                                        ^
      352 |       expect(explore.top_buttons).toBeDefined();
      353 |     });
      354 |

      at test/main.test.ts:351:40
      at fulfilled (node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:164:62)

  ● YouTube.js Tests › YouTube Music › Innertube#music.getAlbum

    Request to https://www.youtube.com/youtubei/v1/browse?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8&prettyPrint=false&alt=json failed with status 404

      144 |       return response;
      145 |     }
    > 146 |     throw new InnertubeError(`Request to ${response.url} failed with status ${response.status}`, await response.text());
          |           ^
      147 |   }
      148 |
      149 |   #adjustContext(ctx: Context, client: string): void {

      at HTTPClient.<anonymous> (src/utils/HTTPClient.ts:146:11)
      at fulfilled (node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:164:62)

  ● YouTube.js Tests › YouTube Music › Innertube#music.getLyrics

    Lyrics not available

      327 |
      328 |     if (page.contents.item().type === 'Message')
    > 329 |       throw new InnertubeError(page.contents.item().as(Message).text.toString(), video_id);
          |             ^
      330 |
      331 |     const section_list = page.contents.item().as(SectionList).contents;
      332 |

      at Music.<anonymous> (src/core/clients/Music.ts:329:13)
      at fulfilled (node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:164:62)

  ● YouTube.js Tests › YouTube Music › Innertube#music.getUpNext

    expect(received).toBeDefined()

    Received: undefined

      384 |     test('Innertube#music.getUpNext', async () => {
      385 |       const upnext = await innertube.music.getUpNext('eaJHysi5tYg');
    > 386 |       expect(upnext).toBeDefined();
          |                      ^
      387 |       expect(upnext?.contents).toBeDefined();
      388 |       expect(upnext?.contents?.length).toBeGreaterThan(0);
      389 |     });

      at test/main.test.ts:386:22
      at fulfilled (node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:164:62)

  ● YouTube.js Tests › YouTube Music › Innertube#music.getRelated

    Cannot cast Message to one of SectionList

      35 |   as<T extends YTNode, K extends YTNodeConstructor<T>[]>(...types: K): InstanceType<K[number]> {
      36 |     if (!this.is(...types)) {
    > 37 |       throw new ParsingError(`Cannot cast ${this.type} to one of ${types.map((t) => t.type).join(', ')}`);
         |             ^
      38 |     }
      39 |     return this;
      40 |   }

      at Message.as (src/parser/helpers.ts:37:13)
      at Music.<anonymous> (src/core/clients/Music.ts:300:42)
      at fulfilled (node_modules/.pnpm/tslib@2.5.0/node_modules/tslib/tslib.js:164:62)

Test Suites: 1 failed, 1 total
Tests:       9 failed, 33 passed, 42 total
Snapshots:   0 total
Time:        31.87 s
Ran all test suites.
ELIFECYCLE  Test failed. See above for more details. 

   And running locally only the HomeFeed#getContinuation test fails (with the same error). Everything else is fine.

byadems commented 4 months ago

for more than 2 weeks, I've been getting the error in the title again for a long time a week. Although I couldn't upgrade to a 9.1.0 version when it made the update, it worked on its own and after a while the same error started happening again. I wanted to give you this information in case it might be useful to you. @LuanRT

absidue commented 4 months ago

@byadems If you are using normal YouTube stuff (so not YouTube Music), the fix is to upgrade to 9.1.0, if it occasionally starts working that's just YouTube messing around but upgrading will definitely fix it.

absidue commented 4 months ago

@Azarattum Definitely sounds like YouTube Music doesn't like your other machine. The only thing I can think of is that maybe the YouTube Music client version that YouTube.js is using for YouTube Music too old, otherwise you might want to change the IP address on the machine your are having issues on (YouTube doesn't like people doing too many requests at the same time, the more you look like a normal human user, the less problems you'll have).

byadems commented 4 months ago

@byadems If you are using normal YouTube stuff (so not YouTube Music), the fix is to upgrade to 9.1.0, if it occasionally starts working that's just YouTube messing around but upgrading will definitely fix it.

I think if I could raise it, I wouldn't write from here. Please do not comment until you have read the written messages. I think I'm being clear.

Azarattum commented 4 months ago

@absidue is there a way to compare/update YouTube Music client version? I'm afraid changing the IP address isn't an option...