【问题标题】:I have an api key but still get a 401 error我有一个 api 密钥,但仍然收到 401 错误
【发布时间】:2020-12-25 12:34:09
【问题描述】:

我的网站无法连接到 OMDB api。这是一个通过 Netlify 部署的 React 应用程序。当我在本地主机 3000 上运行应用程序时,它连接没有问题。非常感谢您提供任何帮助。

这就是我在 App.js 中设置 api 密钥的方式:

searchMovies = async (text) => {
    try {
      const API_KEY = process.env.REACT_APP_API_MOVIE_KEY;
      const response = await axios.get(
        `https://www.omdbapi.com/?apikey=${API_KEY}&`,
        {
          params: { s: text, type: 'movie' },
        }
      );

这是 .env 文件的样子:

REACT_APP_API_MOVIE_KEY=*****

这是 Netlify 上的环境变量的样子:

key: API_KEY value: *****

这是我得到的错误代码:

Failed to load resource: www.omdbapi.com/?apikey=undefined&&s=kate&type=movie:1 Failed to load 
the server responded with a status of 401 ()

【问题讨论】:

  • api_key 未定义..检查 netlify 中的环境变量
  • 您能否确认一下 REACT_APP_API_MOVIE_KEY 是否在 Netlify 上作为环境变量

标签: reactjs api environment-variables netlify


【解决方案1】:

加载资源失败: www.omdbapi.com/?apikey=undefined&&s=kate&type=movie:1加载失败 服务器响应状态为 401 ()

在此错误中,服务器告诉您您的 api 密钥未定义。请记录您的 api 密钥,然后检查

【讨论】:

    【解决方案2】:

    网址中缺少 IMDb ID: 例如http://www.omdbapi.com/?i={imdbid}&apikey={api key}

    【讨论】:

      猜你喜欢
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      相关资源
      最近更新 更多