emresenyuva / youtube-music-api

Node.js Unoffical Youtube Music API
MIT License
117 stars 48 forks source link

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

Open ArakJamman opened 2 years ago

ArakJamman commented 2 years ago

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...

Any fixes for this? Thanks...

TPausL commented 1 year ago

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

RamalS commented 1 year ago

@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

abetwothree commented 1 year ago

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

pigiax commented 1 year ago

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;
}