1

我还应该尝试什么?

我目前正在向 axios 中的 DeepL API 发送请求,但由于 CORS 问题,我收到了 403 响应。

并尝试使用查询字符串设置选项,如此处所示,但它不起作用。https://github.com/funkyremi/deepl/blob/master/index.ts 另外,使用上面 URL 的库返回 403。

此外,DeepL 的账户设置中没有原点设置。

我尝试使用'Content-Type':'application / x-www-form-urlencoded'作为axios标头:{},我还尝试为参数设置选项:{}并且不使用查询字符串,但它们不起作用。

import axios from 'axios'
import querystring from 'querystring';

export const translateDeepL = async() => {
  const options = {
      "auth_key": process.env.DEEPL_AUTH_KEY,
      "text": 'everyday is birthday.',
      "target_lang": 'JA',
  };
  const url = "https://api-free.deepl.com/v2/translate";
  const data = await axios.post(url, querystring.stringify(options)).then(r => r);
  console.log(data);
}
VM3451:1 POST https://api-free.deepl.com/v2/translate 403

将 https 与 ngrok 一起使用的请求也不起作用。

我还尝试了“https://api-free.deepl.com/v2/usage”的 GET 方法,但得到了相同的结果。

绝对是 api-free.deepl.com,因为我使用的是免费计划。

顺便说一句,上面的代码是作为 React 中的一个组件执行的。

4

1 回答 1

0

您可能因为从 http(您的本地主机)向 https 发送请求而被阻止,请尝试使用代理 axios 配置,例如

const response = await axios
.get("https://api-free.deepl.com/v2/translate", {
  params: {
    auth_key: x,
    text: y,
    target_lang: z
  },
  proxy: {
    host: "localhost",
    port: 8080
  }
});

返回响应;};

于 2022-02-15T12:31:12.540 回答