bookfere / Ebook-Translator-Calibre-Plugin

A Calibre plugin to translate ebook into a specified language.
https://translator.bookfere.com
GNU General Public License v3.0
1.54k stars 102 forks source link

[错误报告] deeplx HTTP Error 403 #274

Closed Andy-AO closed 2 months ago

Andy-AO commented 2 months ago

基础信息

使用自定义功能请求 deeplx 远程服务器 HTTP Error 403

image

自定义json

{
    "name": "deeplx(online)",
    "languages": {
        "source": {
            "English": "EN"
        },
        "target": {
            "Chinese": "ZH"
        }
    },
    "request": {
        "url": "https://api.deeplx.org/translate",
        "method": "POST",
        "headers": {
            "Content-Type": "application/json"
        },
        "data": {
            "text": "<text>",
            "source_lang": "<source>",
            "target_lang": "<target>"
        }
    },
    "response": "response['data']"
}

错误日志

Traceback (most recent call last):
  File "calibre_plugins.ebook_translator.components.engine", line 67, in translate_text
  File "calibre_plugins.ebook_translator.engines.custom", line 120, in translate
  File "calibre_plugins.ebook_translator.engines.base", line 209, in get_result
Exception: 无法解析返回的响应。原始数据:

Traceback (most recent call last):
  File "calibre_plugins.ebook_translator.engines.base", line 196, in get_result
  File "mechanize\_mechanize.py", line 257, in open
  File "mechanize\_mechanize.py", line 313, in _mech_open
mechanize._response.get_seek_wrapper_class.<locals>.httperror_seek_wrapper: HTTP Error 403: Forbidden

error code: 1010

额外说明

在python中请求成功,在其他软件中请求也成功,因此不是网络问题

import requests
import json

url = "https://api.deeplx.org/translate"

payload = json.dumps({
  "text": "Hello, world!",
  "source_lang": "auto",
  "target_lang": "ZH"
})
headers = {
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

print("data:")
print(response.json().get("data"))

结果是

D:\text\GitRepository\callDeeplx>  d:; cd 'd:\text\GitRepository\callDeeplx'; & 'd:\text\GitRepository\callDeeplx\Scripts\python.exe' 'c:\Users\Andy\.cursor\extensions\ms-python.python-2023.22.1\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '52427' '--' 'd:\text\GitRepository\callDeeplx\oneline.py' 
{"code":200,"id":8712290002,"data":"你好,世界","alternatives":["世界,你好","你好,世界!","大家好"]}
data:
你好,世界

如果使用本地的deeplx则请求成功

image

bookfere commented 2 months ago

参考内置的 DeepL(Free) 给请求标头添加一个 User-Agent。