extractus / oembed-extractor

Extract oEmbed data from given webpage
https://extractor-demos.pages.dev/oembed-extractor
MIT License
108 stars 43 forks source link

Add error handling for 401: Unauthorized Youtube URLs #168

Closed cmraible closed 1 year ago

cmraible commented 1 year ago

@ndaidong — I recently ran into the same issue detailed in issue 39 (I was unable to re-open the issue), and I found a way to replicate the issue reliably, by using a Youtube URL that has had embedding disabled by the video's author.

For example: https://www.youtube.com/watch?v=5f-JlzBuUUU

Youtube returns 401: Unauthorized when you visit https://www.youtube.com/oembed?format=json&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D5f-JlzBuUUU

It seems that oembed-extractor doesn't check for the 401 error, and simply returns the Unauthorized payload.

Any chance you'd be able to take a look at adding in error handling for this case in the extract(url) function?

Thanks for maintaining this by the way, super helpful for us at Ghost!

ndaidong commented 1 year ago

@cmraible did you mention to Ghost's oembed-service?

As I've checked, it's still using oembed-parser@1.4.9 released since 2021.

When I try to reinstall that version, I got exactly the error as you and Jason have seen (#39).

In v2.x and v3.0.x, oembed-parser silently returns null, without any error threw.

Until 3.1.x, it started handling this case and throwing an error Request failed with error code 401.

At v3.1.6, we've renamed oembed-parser to extractus/oembed-extractor. The behaviour is still the same.

testparser.js:

import { extract } from '@extractus/oembed-extractor'

try {
  const result = await extract('https://www.youtube.com/watch?v=5f-JlzBuUUU')
  console.log(result)
} catch (err) {
  console.error(err.message)
}
// => Request failed with error code 401

So, if that's what you expected, please try to update to the newer version of oembed-parser or replace it with extractus/oembed-extractor. I know that Ghost is a widely used framework and a small change can affect a lot of people. However I'm sure this library core still works in almost the same way as the first versions.