【问题标题】:Empty or malformed authorisation header. Please provide an API key or session token. 400 bad request error空的或格式错误的授权标头。请提供 API 密钥或会话令牌。 400 错误请求错误
【发布时间】:2021-08-30 17:27:29
【问题描述】:

我正在构建一个食品检测应用程序,并且我已经尝试了所有方法来消除这个错误!粘贴 url 时收到 400 bad request 错误。

这是显示错误的状态码:

> {"status":{"code":11102,"description":"Invalid request","details":"Empty or malformed authorization header. Please provide an API key or session token.","req_id":"7d399e162bb943f5848960862ccedbe9"}}

我的 App.js 是这个(没有包含导入部分和 api 密钥):

      const app = new Clarifai.App({
          apiKey: "MY API KEY",
        });
        
        const particleOptions = {
          particles: {
            number: {
              value: 100,
              density: {
                enable: true,
                value_area: 800,
              },
            },
          },
        };
        
        class App extends Component {
          constructor() {
            super();
            this.state = {
              input: "",
              imageUrl: "",
              showModal: false,
              foodIngredients: [],
            };
          }
    
      onInputChange = (event) => {
        this.setState({ input: event.target.value });
      };
    
         onButtonSubmit = () => {
                this.setState({ imageUrl: this.state.input, showModal: true });
                app.models
                  .predict(
                    {
                      model_id: " bd367be194cf45149e75f01d59f77ba7",
                      version_id: "dfebc169854e429086aceb8368662641",
                    },
                    this.state.input
                  )
                  .then((response) => {
                    console.log("hi", response);
                    if (response) {
                      fetch("http://localhost:3000/image/", {
                        method: "put",
                        headers: { "Content-Type": "application/json" },
                        body: JSON.stringify({
                          url: this.state.input,
                        }),
                      })
                        .then((response) => response.json())
                        .then((response) => {
                          //response ingredient array
                          if (response.length <= 0) {
                            return this.setState({
                              input: "",
                              foodIngredients: [response],
                            });
                          } else {
                            this.setState({
                              input: "",
                              foodIngredients: response,
                            });
                          }
                        });
                    }
                  })
                  .catch((err) => {
                    console.log(err);
                    this.setState({ imageUrl: "" });
                  });
              };

  onModalShow = () => {
    this.setState({ showModal: false, foodIngredients: [] });
  };

  render() {
    const { input, imageURL, showModal, foodIngredients } = this.state;
    return (
      <div className="App">
        <Particles className="particles" params={particleOptions} />
        <div>
          <Logo />
          <ImageLinkForm
            urlInput={this.onInputChange}
            text={input}
            buttonClick={this.onButtonSubmit}
          />
          <FoodRecognition
            url={imageURL}
            show={showModal}
            canceled={this.onModalShow}
            foodIngredients={foodIngredients}
          />
        </div>
      </div>
    );
  }
}

export default App;

如果我需要添加或更改任何内容,请告诉我。

更新 更改 .predict 时出现新错误

app.models
      .predict(
        Clarifai.FOOD_MODEL,
        // {
        //   model_id: " bd367be194cf45149e75f01d59f77ba7",
        //   version_id: "dfebc169854e429086aceb8368662641",
        // },
        this.state.input
      )

错误:

App.js:55 PUT http://localhost:3000/image/ 404 (Not Found)
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

【问题讨论】:

  • 您显示的代码未显示对https://api.clarifai.com/v2/models/searches 的请求,这是错误的来源。请显示与实际错误对应的代码。
  • 并且,在您的问题中包含代码作为文本(而不是图像),并正确格式化为代码。这是在 Stackoverflow 上发布问题的基准程序。由于各种原因,代码永远不应该出现在 Stackoverflow 上的图像中。它无法搜索。试图帮助的人无法复制/粘贴到答案中,外部图像习惯于随着时间的推移而消失,使得问题无法作为长期参考,等等......
  • 抱歉,我已将问题(代码)编辑为文本。
  • 当我在控制台中按下我的 app.js 时。它需要我去 .catch((err)。所以我只是粘贴了整个代码,认为它可以帮助人们理解我想要做什么。
  • 您在代码中直接显示的唯一实际 HTTP 请求是与错误消息不对应的 localhost。错误来自app.models.predict().catch(...)?那是错误显示的.catch()吗?如果是这样,那么我猜它来自 Clarifai 库内部,所以问题将出在您传递给它的参数中。

标签: node.js http-status-code-400 bad-request clarifai


【解决方案1】:

【讨论】:

  • 问题是我在控制台中得到响应。但是当我获取 URL 时,我无法在网站上显示图像
  • 我收到两个错误。一:&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Error&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;pre&gt;Cannot PUT /images&lt;/pre&gt; &lt;/body&gt; &lt;/html&gt;
  • 另一位:fetch("http://localhost:3000/images",在这一行
猜你喜欢
  • 1970-01-01
  • 2013-07-21
  • 2020-07-13
  • 2014-02-18
  • 2017-06-22
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
  • 2016-12-03
相关资源
最近更新 更多