using fetch(url) to get xml (youtube feed)
-
I'd like to be able to fetch data from my youtube channel's rss feed to display dynamic content on my website... So, I have my youtube channel rss feed address, something like: https://www.youtube.com/feeds/videos.xml?channel\_id=n0tMyRealcHaNNel1D I'm able to us wget in bash to download the data into a file, and obviously I can load the xml in my browser, but am getting stuck fetching the data in js.
fetch("https://www.youtube.com/feeds/videos.xml?channel\_id=n0tMyRealcHaNNel1D")
.then(response => response.text())
.then(data => {
const parser = new DOMParser();
const xml = parser.parseFromString(data, "application/xml");
console.log(xml);
})
.catch(console.error);gives me:
Access to fetch at 'https://www.youtube.com/feeds/videos.xml?channel\_id=n0tMyRealcHaNNel1D' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
custom.js:3 GET https://www.youtube.com/feeds/videos.xml?channel\_id=n0tMyRealcHaNNel1D net::ERR_FAILED 200
(anonymous) @ custom.js:3
TypeError: Failed to fetch
at custom.js:3:1having looked into this further, I discovered this: [https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9\](https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9) so I tried prepending the proxy [https://cors-anywhere.herokuapp.com/\](https://cors-anywhere.herokuapp.com/) to the fetch url. Now I get:
custom.js:3 GET https://cors-anywhere.herokuapp.com/https://www.youtube.com/feeds/videos.xml?channel\_id=n0tMyRealcHaNNel1D 403 (Forbidden)
So no joy there. The final option mentioned in the medium article above is to create a proxy for myself... but before heading down that road (especially if it's going to be a waste of time!), I thought i'd ask here in case anyone has any advice for me. Maybe I'm barking up the wrong tree and need to use google API and php? (a daunting prospect for me!)