错误:状态代码403的请求失败 - 与Axios的问题,可能的交换以获取
我正在使用youtube API的应用程序中使用Axios,并且发生以下错误发生
以下是代码,
import React from "react";
import youtube from "./api";
import VideoList from "./VideoList";
class App extends React.Component {
state = {
videos: [],
};
componentDidMount() {
this.onTermSubmit("Car");
}
onTermSubmit = async (term) => {
const res = await youtube.get("/search", {
params: {
q: term,
},
});
this.setState({ videos: res.data.items });
};
render() {
return (
<>
<div>
<div >
<VideoList
videos={this.state.videos}
/>
</div>
</div>
</>
);
}
}
export default App;
另一个
import axios from 'axios';
const KEY = "xxxxx";
export default axios.create({
baseURL: "https://www.googleapis.com/youtube/v3",
params: {
part: "snippet",
maxResults: 16,
key: KEY,
},
});
我想提供有关如何解决此问题的提示,如果最好的解决方案是最好的解决方案从Axios更改为获取。问题是,我不知道如何从Axios变为获取。
I'm using axios in an application Nextjs where I use the Youtube API and the following error occurred
Below is the code
import React from "react";
import youtube from "./api";
import VideoList from "./VideoList";
class App extends React.Component {
state = {
videos: [],
};
componentDidMount() {
this.onTermSubmit("Car");
}
onTermSubmit = async (term) => {
const res = await youtube.get("/search", {
params: {
q: term,
},
});
this.setState({ videos: res.data.items });
};
render() {
return (
<>
<div>
<div >
<VideoList
videos={this.state.videos}
/>
</div>
</div>
</>
);
}
}
export default App;
The other
import axios from 'axios';
const KEY = "xxxxx";
export default axios.create({
baseURL: "https://www.googleapis.com/youtube/v3",
params: {
part: "snippet",
maxResults: 16,
key: KEY,
},
});
I would like a tip on how to solve this and if the best solution would be to change from Axios to fetch. Problem is, I don't know how to change from Axios to fetch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这与包装无关。因为403是禁止响应状态代码,这表明服务器了解请求,但拒绝授权。您很可能通过错误的API键。如果您需要使用
fetch
获取数据,则可以编写一个可重复使用的函数:这是来自 docs
如果在测试样本时遇到401或403错误,则收到
401或403的错误,这很可能是由于以下一个问题引起的:
I dont think that is related to package. Because 403 is forbidden response status code which indicates that the server understands the request but refuses to authorize it. Most likely you pass the wrong api key. If you need to fetch the data with
fetch
, you can write a reusable function:this is from docs
Received a 401 or 403 error
If you're getting a 401 or 403 error when testing a sample, it's likely due to a problem with one of the following:
降级到Axios 0.26.0
您可能会自动使用最后版本。
有关的:
https://github.com/axios/axios/axios/assues/issues/4638
Downgrade to AXIOS 0.26.0
Probably you are using last version automatically.
Related:
https://github.com/axios/axios/issues/4638
我在Next.js 13 vs Django Rest框架中遇到了类似的问题,我花了一个星期才发现(就我而言)的问题来自Axios。在获取并将文件上传到AWS S3存储桶时,我遇到了403个禁止错误。我花了将近五天的时间来解决我的获取和发布请求,而不是axios。这是出乎意料的,我无法解释原因。我怀疑Axios设置了错误的默认身份验证标头,即使使用标志:
withCredentials:false。
这仅在生产中发生而不是在开发中发生。I encountered a similar issue in Next.js 13 vs Django rest framework and it took me a week to discover that the problem (in my case) came from Axios. I was getting 403 forbidden errors when fetching and uploading files to AWS S3 Bucket. It took me almost five days to solve by just using fetch for my GET and POST requests, instead of Axios. It's unexpected though and I can't explain why. I suspected Axios was setting a wrong default Authentication header, even with the flag:
withCredentials: false.
This only happened in production and not in development.