添加授权列表
This commit is contained in:
@@ -187,24 +187,31 @@ class DouyinRequest:
|
||||
url: str,
|
||||
method: str = 'GET',
|
||||
options: Optional[Dict[str, Any]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
) -> Any:
|
||||
options = options or {}
|
||||
token = await self.get_access_token(oauth_id)
|
||||
|
||||
for i in range(1, 6):
|
||||
for i in range(1, 2):
|
||||
try:
|
||||
headers = options.get('headers', {}).copy()
|
||||
headers['Access-Token'] = token
|
||||
headers.setdefault('Content-Type', 'application/json')
|
||||
|
||||
has_files = 'files' in options
|
||||
if has_files:
|
||||
# 移除可能错误设置的 Content-Type,让库自动生成 multipart 头
|
||||
headers.pop('Content-Type', None)
|
||||
else:
|
||||
headers.setdefault('Content-Type', 'application/json')
|
||||
|
||||
options['headers'] = headers
|
||||
|
||||
|
||||
response = await self.client.request(method, url, **options)
|
||||
response.raise_for_status()
|
||||
|
||||
|
||||
try:
|
||||
data = response.json()
|
||||
except json.JSONDecodeError:
|
||||
data = {'code': 0, 'data': response.text}
|
||||
data = {'code': 0, 'data': response.text, 'msg':'JSON解析失败'}
|
||||
|
||||
if 'code' not in data:
|
||||
await asyncio.sleep(i * 5)
|
||||
@@ -236,9 +243,18 @@ class DouyinRequest:
|
||||
continue
|
||||
|
||||
res = json.dumps(data) if 'data' in locals() else ''
|
||||
|
||||
options_log = {}
|
||||
if options:
|
||||
for key, value in options.items():
|
||||
if key == 'files':
|
||||
options_log[key] = {k: (v[0], 'bytes_content', v[2]) for k, v in value.items()}
|
||||
else:
|
||||
options_log[key] = value
|
||||
|
||||
raise RuntimeError(
|
||||
f'DouYin API request failed after 5 retries. '
|
||||
f'url:{url};oauthId:{oauth_id};options:{json.dumps(options)};response:{res}'
|
||||
f'url:{url};oauthId:{oauth_id};options:{json.dumps(options_log)};response:{res}'
|
||||
)
|
||||
|
||||
# 无token请求
|
||||
@@ -250,7 +266,7 @@ class DouyinRequest:
|
||||
) -> Dict[str, Any]:
|
||||
options = options or {}
|
||||
|
||||
for i in range(1, 6):
|
||||
for i in range(1, 2):
|
||||
try:
|
||||
headers = options.get('headers', {}).copy()
|
||||
headers.setdefault('Content-Type', 'application/json')
|
||||
|
||||
Reference in New Issue
Block a user