Repository: ytdl-org/youtube-dl

Issue:
[bilibili] error 466 in downloading
### Make sure you are using the *latest* version: run `youtube-dl --version` and ensure your version is *2017.12.31*. If it's not, read [this FAQ entry](https://github.com/rg3/youtube-dl/blob/master/README.md#how-do-i-update-youtube-dl) and update. Issues with outdated version will be rejected.
- [x] I've **verified** and **I assure** that I'm running youtube-dl **2017.12.31**

### What is the purpose of your *issue*?
- [x] Bug report (encountered problems with youtube-dl)
- [ ] Site support request (request for adding support for a new site)
- [ ] Feature request (request for a new functionality)
- [ ] Question
- [ ] Other

---

Hello,i try to use youtube-dl ver12.31 to download a bilibili video but i get a 403 error

youtube-dl -F http://www.bilibili.com/video/av17843872/
[BiliBili] 17843872: Downloading webpage
[BiliBili] 17843872: Downloading video info page
ERROR: Unable to download JSON metadata: HTTP Error 403: Forbidden (caused by HT
TPError()); please report this issue on https://yt-dl.org/bug . Make sure you ar
e using the latest version; type  youtube-dl -U  to update. Be sure to call yout
ube-dl with the --verbose flag and include its complete output.
[bilibili] error 466 in downloading
### Make sure you are using the *latest* version: run `youtube-dl --version` and ensure your version is *2017.12.31*. If it's not, read [this FAQ entry](https://github.com/rg3/youtube-dl/blob/master/README.md#how-do-i-update-youtube-dl) and update. Issues with outdated version will be rejected.
- [x] I've **verified** and **I assure** that I'm running youtube-dl **2017.12.31**

### What is the purpose of your *issue*?
- [x] Bug report (encountered problems with youtube-dl)
- [ ] Site support request (request for adding support for a new site)
- [ ] Feature request (request for a new functionality)
- [ ] Question
- [ ] Other

---

Hello,i try to use youtube-dl ver12.31 to download a bilibili video but i get a 403 error

youtube-dl -F http://www.bilibili.com/video/av17843872/
[BiliBili] 17843872: Downloading webpage
[BiliBili] 17843872: Downloading video info page
ERROR: Unable to download JSON metadata: HTTP Error 403: Forbidden (caused by HT
TPError()); please report this issue on https://yt-dl.org/bug . Make sure you ar
e using the latest version; type  youtube-dl -U  to update. Be sure to call yout
ube-dl with the --verbose flag and include its complete output.


Discussion:
Same here
log: https://ghostbin.com/paste/yjub6
  
I have the same problem
I've verified and I assure that I'm running youtube-dl 2017.12.31
Hi, I am experiencing the same issue...my youtube-dl is also updated to 2017.12.31 already...anyone can advise what to do with this issue?
Could you try to include --referer and see if that works?  
So the command becomes:  
`youtube-dl -F http://www.bilibili.com/video/av17843872/ --referer http://www.bilibili.com/video/av17843872/`

For me, this fixes the issue, see log: https://ghostbin.com/paste/byuk2

@gynt Hi there! Thanks so much for your advice it works for me as well! 
Same here
log: https://ghostbin.com/paste/yjub6
  
I have the same problem
I've verified and I assure that I'm running youtube-dl 2017.12.31
Hi, I am experiencing the same issue...my youtube-dl is also updated to 2017.12.31 already...anyone can advise what to do with this issue?
Could you try to include --referer and see if that works?  
So the command becomes:  
`youtube-dl -F http://www.bilibili.com/video/av17843872/ --referer http://www.bilibili.com/video/av17843872/`

For me, this fixes the issue, see log: https://ghostbin.com/paste/byuk2

@gynt Hi there! Thanks so much for your advice it works for me as well! 

Resolving patch:
diff --git a/youtube_dl/extractor/bilibili.py b/youtube_dl/extractor/bilibili.py
--- a/youtube_dl/extractor/bilibili.py
+++ b/youtube_dl/extractor/bilibili.py
@@ -102,6 +102,7 @@ def _real_extract(self, url):
                     video_id, anime_id, compat_urlparse.urljoin(url, '//bangumi.bilibili.com/anime/%s' % anime_id)))
             headers = {
                 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
+                'Referer': url
             }
             headers.update(self.geo_verification_headers())
 
@@ -116,10 +117,15 @@ def _real_extract(self, url):
         payload = 'appkey=%s&cid=%s&otype=json&quality=2&type=mp4' % (self._APP_KEY, cid)
         sign = hashlib.md5((payload + self._BILIBILI_KEY).encode('utf-8')).hexdigest()
 
+        headers = {
+            'Referer': url
+        }
+        headers.update(self.geo_verification_headers())
+
         video_info = self._download_json(
             'http://interface.bilibili.com/playurl?%s&sign=%s' % (payload, sign),
             video_id, note='Downloading video info page',
-            headers=self.geo_verification_headers())
+            headers=headers)
 
         if 'durl' not in video_info:
             self._report_error(video_info)


Test patch: