youtube-music-api icon indicating copy to clipboard operation
youtube-music-api copied to clipboard

Invalid value "undefined" for header "X-Goog-Visitor-Id"

Open ArakJamman opened this issue 2 years ago • 5 comments

This is a very useful package, but I am having some problems when searching with the api. It seems like this issue was opened and closed in https://github.com/emresenyuva/youtube-music-api/commit/b52b2b0d3fd146face64d193ba333c948e857451, but I am still having this issue...

  • When I use the api like so, the search is successful: image

  • However, when I use the api like this, the search is unsuccessful: image image

Any fixes for this? Thanks...

ArakJamman avatar Aug 31 '22 01:08 ArakJamman

i guess https://github.com/emresenyuva/youtube-music-api/commit/b52b2b0d3fd146face64d193ba333c948e857451 wasn't published on npm could @emresenyuva, @wilsonpage or @valeriangalliat do that?

TPausL avatar Sep 20 '22 18:09 TPausL

@ArakJamman @mcitomiapplecat I had the same problem. For now, go to node_modules/youtube-music-api/src/index.js and replace line 49 with 'X-Goog-Visitor-Id': this.ytcfg.VISITOR_DATA || ''. image

RamalS avatar Oct 13 '22 14:10 RamalS

The issues looks to be that you need to wait for api.initialize to finish it's HTTP request. Initialize actually pings YouTube music to grab session cookies in order to be able to ping the private API so you can't make any API requests until initialize finishes grabbing session cookies.

Recommend using async/await pattern so you can do this:

await api.initizalize();
const result = await api.search('search term', 'song', 1);

I created a small REST api app for this package where you can see me doing the async/await stuff. https://github.com/abetwothree/youtube-music-rest-api-microservice

abetwothree avatar Mar 06 '23 20:03 abetwothree

Hi, there is a workaround if you are not able to make it work:

const YouTubeMusicAPI = require('youtube-music-api');

const youtubeMusic = new YouTubeMusicAPI();

async function searchForSong(title, artist) {
    await youtubeMusic.initalize();
    youtubeMusic.ytcfg.VISITOR_DATA = '';
    const searchResult = await youtubeMusic.search(`${title} ${artist}`);
    if (searchResult && searchResult.content) {
        const songId = searchResult.content[0].videoId;
        const songUrl = `https://www.youtube.com/watch?v=${songId}`;
        console.log(songUrl);
        return songUrl;
    }
    return null;
}

pigiax avatar Mar 09 '23 23:03 pigiax