XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/bilibili-API-collect

feat: bili_ticket (#932)

180b8ce
z0z0r4 <z0z0r4@outlook.com>
提交于

代码差异

1 个文件 +62 -0
Added docs/misc/sign/bili_ticket.md +62 -0
@@ -0,0 +1,62 @@
1 `bili_ticket` 目前没发现多少风控价值,但是暂且在这里提供一份示例。
2
3 由 [@aynuarance](https://github.com/aynuarance) 于 [#903](https://github.com/SocialSisterYi/bilibili-API-collect/issues/903) 提供的思路,根据时间戳使用 `hmac_sha256` 算法计算 `hexsign`。
4
5
6 是 [JWT 令牌](https://jwt.io/),有效时长为 259260 秒,即 3 天。
7 例如 `eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDI3NDI3NDYsImlhdCI6MTcwMjQ4MzQ4NiwicGx0IjotMX0.xQgtTAc41NA1gzvd9yKUPgucUy_DKcQj6OG1vj8V7ZA`
8
9 ```json
10 {
11 "alg": "HS256",
12 "kid": "s03",
13 "typ": "JWT"
14 }
15 ```
16
17 # Python 示例
18
19 ```python
20 import hmac
21 import hashlib
22 import requests
23 import time
24
25 def hmac_sha256(key, message):
26 """
27 使用HMAC-SHA256算法对给定的消息进行加密
28 :param key: 密钥
29 :param message: 要加密的消息
30 :return: 加密后的哈希值
31 """
32 # 将密钥和消息转换为字节串
33 key = key.encode('utf-8')
34 message = message.encode('utf-8')
35
36 # 创建HMAC对象,使用SHA256哈希算法
37 hmac_obj = hmac.new(key, message, hashlib.sha256)
38
39 # 计算哈希值
40 hash_value = hmac_obj.digest()
41
42 # 将哈希值转换为十六进制字符串
43 hash_hex = hash_value.hex()
44
45 return hash_hex
46
47
48 if __name__ == '__main__':
49 o = hmac_sha256("XgwSnGZ1p",f"ts{int(time.time())}")
50 url = "https://api.bilibili.com/bapis/bilibili.api.ticket.v1.Ticket/GenWebTicket"
51 params = {
52 "key_id":"ec02",
53 "hexsign":o,
54 "context[ts]":f"{int(time.time())}",
55 "csrf": ''
56 }
57
58 headers = {
59 'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0"
60 }
61 resp = requests.post(url, params=params,headers=headers).json()
62 ```