mirror of
https://github.com/SocialSisterYi/bilibili-API-collect.git
synced 2025-03-30 01:40:08 +08:00
修复Demo访问/x/web-interface/nav被ban的问题 (#991)
尝试向demo代码添加referer头和user-agent来解决被ban
This commit is contained in:
parent
3802052be8
commit
64e1bde5be
@ -163,7 +163,11 @@ def encWbi(params: dict, img_key: str, sub_key: str):
|
|||||||
|
|
||||||
def getWbiKeys() -> tuple[str, str]:
|
def getWbiKeys() -> tuple[str, str]:
|
||||||
'获取最新的 img_key 和 sub_key'
|
'获取最新的 img_key 和 sub_key'
|
||||||
resp = requests.get('https://api.bilibili.com/x/web-interface/nav')
|
headers = {
|
||||||
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
|
||||||
|
'Referer': 'https://www.bilibili.com/'
|
||||||
|
}
|
||||||
|
resp = requests.get('https://api.bilibili.com/x/web-interface/nav', headers=headers)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
json_content = resp.json()
|
json_content = resp.json()
|
||||||
img_url: str = json_content['data']['wbi_img']['img_url']
|
img_url: str = json_content['data']['wbi_img']['img_url']
|
||||||
@ -240,7 +244,9 @@ async function getWbiKeys() {
|
|||||||
const res = await fetch('https://api.bilibili.com/x/web-interface/nav', {
|
const res = await fetch('https://api.bilibili.com/x/web-interface/nav', {
|
||||||
headers: {
|
headers: {
|
||||||
// SESSDATA 字段
|
// SESSDATA 字段
|
||||||
Cookie: "SESSDATA=xxxxxx"
|
Cookie: "SESSDATA=xxxxxx",
|
||||||
|
User-Agent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
|
||||||
|
Referer: 'https://www.bilibili.com/'//对于直接浏览器调用可能不适用
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const { data: { wbi_img: { img_url, sub_url } } } = await res.json()
|
const { data: { wbi_img: { img_url, sub_url } } } = await res.json()
|
||||||
@ -322,6 +328,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
|
||||||
|
req.Header.Set("Referer", "https://www.bilibili.com/")
|
||||||
response, err := http.DefaultClient.Do(req)
|
response, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Request failed: %s", err)
|
fmt.Printf("Request failed: %s", err)
|
||||||
@ -423,15 +430,23 @@ func getWbiKeysCached() (string, string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getWbiKeys() (string, string) {
|
func getWbiKeys() (string, string) {
|
||||||
resp, err := http.Get("https://api.bilibili.com/x/web-interface/nav")
|
client := &http.Client{}
|
||||||
|
req, err := http.NewRequest("GET", "https://api.bilibili.com/x/web-interface/nav", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error: %s", err)
|
fmt.Printf("Error creating request: %s", err)
|
||||||
|
return "", ""
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
|
||||||
|
req.Header.Set("Referer", "https://www.bilibili.com/")
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error sending request: %s", err)
|
||||||
return "", ""
|
return "", ""
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error: %s", err)
|
fmt.Printf("Error reading response: %s", err)
|
||||||
return "", ""
|
return "", ""
|
||||||
}
|
}
|
||||||
json := string(body)
|
json := string(body)
|
||||||
@ -497,7 +512,11 @@ class Program
|
|||||||
// 获取最新的 img_key 和 sub_key
|
// 获取最新的 img_key 和 sub_key
|
||||||
private static async Task<(string, string)> GetWbiKeys()
|
private static async Task<(string, string)> GetWbiKeys()
|
||||||
{
|
{
|
||||||
HttpResponseMessage responseMessage = await _httpClient.SendAsync(new HttpRequestMessage
|
var httpClient = new HttpClient();
|
||||||
|
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
|
||||||
|
httpClient.DefaultRequestHeaders.Referrer = new Uri("https://www.bilibili.com/");
|
||||||
|
|
||||||
|
HttpResponseMessage responseMessage = await httpClient.SendAsync(new HttpRequestMessage
|
||||||
{
|
{
|
||||||
Method = HttpMethod.Get,
|
Method = HttpMethod.Get,
|
||||||
RequestUri = new Uri("https://api.bilibili.com/x/web-interface/nav"),
|
RequestUri = new Uri("https://api.bilibili.com/x/web-interface/nav"),
|
||||||
@ -513,6 +532,7 @@ class Program
|
|||||||
return (imgUrl, subUrl);
|
return (imgUrl, subUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static async Task Main()
|
public static async Task Main()
|
||||||
{
|
{
|
||||||
var (imgKey, subKey) = await GetWbiKeys();
|
var (imgKey, subKey) = await GetWbiKeys();
|
||||||
@ -584,6 +604,8 @@ public class WbiTest {
|
|||||||
map.put("bar", "五一四");
|
map.put("bar", "五一四");
|
||||||
map.put("baz", 1919810);
|
map.put("baz", 1919810);
|
||||||
map.put("wts", System.currentTimeMillis() / 1000);
|
map.put("wts", System.currentTimeMillis() / 1000);
|
||||||
|
map.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
|
||||||
|
map.put("Referer", "https://www.bilibili.com/");
|
||||||
StringJoiner param = new StringJoiner("&");
|
StringJoiner param = new StringJoiner("&");
|
||||||
//排序 + 拼接字符串
|
//排序 + 拼接字符串
|
||||||
map.entrySet().stream()
|
map.entrySet().stream()
|
||||||
@ -679,6 +701,7 @@ class Bilibili {
|
|||||||
$header[] = "Accept: */*";
|
$header[] = "Accept: */*";
|
||||||
$header[] = "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7";
|
$header[] = "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7";
|
||||||
$header[] = "Connection: close";
|
$header[] = "Connection: close";
|
||||||
|
$header[]="Referer:https://www.bilibili.com/";
|
||||||
$header[] = "Cache-Control: max-age=0";
|
$header[] = "Cache-Control: max-age=0";
|
||||||
curl_setopt_array($ch, [
|
curl_setopt_array($ch, [
|
||||||
CURLOPT_HTTPGET => 1,
|
CURLOPT_HTTPGET => 1,
|
||||||
@ -796,6 +819,7 @@ async fn get_wbi_keys() -> Result<(String, String), reqwest::Error> {
|
|||||||
let ResWbi { data:Data{wbi_img} } = client
|
let ResWbi { data:Data{wbi_img} } = client
|
||||||
.get("https://api.bilibili.com/x/web-interface/nav")
|
.get("https://api.bilibili.com/x/web-interface/nav")
|
||||||
.header(USER_AGENT,"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
|
.header(USER_AGENT,"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
|
||||||
|
.header("Referer","https://www.bilibili.com/")
|
||||||
// SESSDATA=xxxxx
|
// SESSDATA=xxxxx
|
||||||
.header("Cookie", "SESSDATA=xxxxx")
|
.header("Cookie", "SESSDATA=xxxxx")
|
||||||
.send()
|
.send()
|
||||||
@ -850,7 +874,12 @@ func biliWbiSign(param: String, completion: @escaping (String?) -> Void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getWbiKeys(completion: @escaping (Result<(imgKey: String, subKey: String), Error>) -> Void) {
|
func getWbiKeys(completion: @escaping (Result<(imgKey: String, subKey: String), Error>) -> Void) {
|
||||||
AF.request("https://api.bilibili.com/x/web-interface/nav").responseJSON { response in
|
let headers: HTTPHeaders = [
|
||||||
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
|
||||||
|
"Referer": "https://www.bilibili.com/"
|
||||||
|
]
|
||||||
|
|
||||||
|
AF.request("https://api.bilibili.com/x/web-interface/nav", headers: headers).responseJSON { response in
|
||||||
switch response.result {
|
switch response.result {
|
||||||
case .success(let value):
|
case .success(let value):
|
||||||
let json = JSON(value)
|
let json = JSON(value)
|
||||||
@ -865,6 +894,7 @@ func biliWbiSign(param: String, completion: @escaping (String?) -> Void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func calculateMD5(string: String) -> String {
|
func calculateMD5(string: String) -> String {
|
||||||
let data = Data(string.utf8)
|
let data = Data(string.utf8)
|
||||||
var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
|
var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
|
||||||
|
Loading…
Reference in New Issue
Block a user