philbot9 / youtube-info

Fetch meta information about YouTube videos
ISC License
43 stars 20 forks source link

An Error #4

Closed ishid4 closed 6 years ago

ishid4 commented 6 years ago

I'm getting this error, but the code is working well... Just having this error.

Unhandled rejection Error: Error: StatusCodeError: 403 - "" at C:\Users\Macbeth\Documents\Gardener\index.js:308:22 at C:\Users\Macbeth\Documents\Gardener\node_modules\youtube-info\index.js:35:9 at tryCatcher (C:\Users\Macbeth\Documents\Gardener\node_modules\request-promise\node_modules\bluebird\js\release\util.js:16:23) at Promise._settlePromiseFromHandler (C:\Users\Macbeth\Documents\Gardener\node_modules\request-promise\node_modules\bluebird\js\release\promise.js:512:31) at Promise._settlePromise (C:\Users\Macbeth\Documents\Gardener\node_modules\request-promise\node_modules\bluebird\js\release\promise.js:569:18) at Promise._settlePromise0 (C:\Users\Macbeth\Documents\Gardener\node_modules\request-promise\node_modules\bluebird\js\release\promise.js:614:10) at Promise._settlePromises (C:\Users\Macbeth\Documents\Gardener\node_modules\request-promise\node_modules\bluebird\js\release\promise.js:689:18) at Async._drainQueue (C:\Users\Macbeth\Documents\Gardener\node_modules\request-promise\node_modules\bluebird\js\release\async.js:133:16) at Async._drainQueues (C:\Users\Macbeth\Documents\Gardener\node_modules\request-promise\node_modules\bluebird\js\release\async.js:143:10) at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\Macbeth\Documents\Gardener\node_modules\request-promise\node_modules\bluebird\js\release\async.js:17:14) at runCallback (timers.js:757:18) at tryOnImmediate (timers.js:718:5) at processImmediate [as _immediateCallback] (timers.js:698:5)

SteeledSlagle13 commented 6 years ago

I am having the same issue. It occurs when I am iterating through multiple {videoID}s.

Probably not the best thing to do, but I commented out line 94 in index.js -> throw new Error(reason)

devesh2605 commented 6 years ago

@SteeledSlagle13 Did you find a solution for the error?

SteeledSlagle13 commented 6 years ago

@devesh2605 I just commented out line 94 in the index.js file in the youtube-info package

//... Line 86
      .then(extractCommentCount)
      .catch(function (reason) {
        debug(
          'Fetching comment page failed %d - %s',
          reason.statusCode,
          reason.error
        )
//      throw new Error(reason)                 This is line 94
      })
}
//Line 97 ...

After commenting that line out my issue was resolved... I am not too sure what was causing that error to be throw, but now I am getting all the data I need.

philbot9 commented 6 years ago

Well looking at the error message it would appear that a network request has failed with a 403 error. This occurs in the function that fetches the comment count for a video. So that information will be missing if you comment out line 94.

Can one of you please provide some code that I can use to reproduce the error?

devesh2605 commented 6 years ago

Usually 403 refers to unauthorised calls, when we try to fetch details of multiple videos through loop the server rejects the call with 403 status code..

philbot9 commented 6 years ago

@devesh2605 Thanks, I get that. Without any code to reproduce this I can't really find a solution for it though :wink:

Can you please post your code that's causing this error so I can try to reproduce it?

SteeledSlagle13 commented 6 years ago

Just did a clean install, and grabbed ids from videos.

var fetchVideoInfo = require('youtube-info');
var videoID = ['xPU8OAjjS4k', 'BJk6gZuPKRE', 'b8-tXG8KrWs', 'NU9JoFKlaZ0', '7QU1nvuxaMA', 'Mlahvvymkxc', 'uwIGZLjugKA'];
videoID.forEach(video => {
    fetchVideoInfo(video).then(function (videoInfo) {
        console.log(videoInfo.url);
    });
});

That example code returns the error 403 6 times.

Then after commenting line 94

gives me

https://www.youtube.com/watch?v=Mlahvvymkxc
https://www.youtube.com/watch?v=NU9JoFKlaZ0
https://www.youtube.com/watch?v=xPU8OAjjS4k
https://www.youtube.com/watch?v=7QU1nvuxaMA
https://www.youtube.com/watch?v=BJk6gZuPKRE
https://www.youtube.com/watch?v=uwIGZLjugKA
https://www.youtube.com/watch?v=b8-tXG8KrWs
philbot9 commented 6 years ago

Thank you @SteeledSlagle13.

I've implemented a fix in https://github.com/philbot9/youtube-info/commit/10b5991eb32df021c03380f1be723f8c7809abd8 and published it to npm in version 1.2.1. So you can go ahead and update.

The problem was that the request module by default shares the same cookie jar between subsequent requests (as it should). YouTube requires certain cookies for requests to the same videoId. When iterating with Array.forEach() you are actually fetching the video info in parallel and thus the cookies stored in the cookie jar become invalid and YouTube gives us a 403. I've changed the function to use a separate cookie jar for every function call, so the cookies stay consistent between requests.