youtube-music-api
youtube-music-api copied to clipboard
Invalid value "undefined" for header "X-Goog-Visitor-Id"
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:
-
However, when I use the api like this, the search is unsuccessful:
Any fixes for this? Thanks...
i guess https://github.com/emresenyuva/youtube-music-api/commit/b52b2b0d3fd146face64d193ba333c948e857451 wasn't published on npm could @emresenyuva, @wilsonpage or @valeriangalliat do that?
@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 || ''.
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
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;
}