我正在尝试使用Rapid API的youtube API,但当我使用Axios发送请求时,我得到了403&429个错误【错误】[1]
这是我的API fetchData
import axios from "axios";
const BASE_URL = "https://youtube-v31.p.rapidapi.com";
const options = {
params: {
maxResults: "50",
},
headers: {
"X-RapidAPI-Key": process.env.REACT_APP_RAPID_API_KEY,
"X-RapidAPI-Host": "youtube-v31.p.rapidapi.com",
},
};
export const fetchFromAPI = async (url) => {
const { data } = await axios.get(`${BASE_URL}/${url}`, options);
return data;
};
这是我试图发送请求的时候
import React from "react";
import { useEffect, useState } from "react";
import { Box, Stack, Typography } from "@mui/material";
import { Sidebar, Videos } from "./index";
import { fetchFromAPI } from "../utils/fetchFromAPI";
const Feed = () => {
const [selectedCategory, setSelectedCategory] = useState("New");
const [videos, setVideos] = useState([]);
useEffect(() => {
fetchFromAPI(`search?part=snippet&q=${selectedCategory}`).then((data) =>
setVideos(data.items)
);
}, [selectedCategory]);
return (
<Stack sx={{ flexDirection: { sx: "column", md: "row" } }}>
<Box
sx={{
height: { sx: "auto", md: "92vh" },
borderRight: "1px solid #3d3d3d",
paddingX: { sx: 0, md: 2 },
}}
>
<Sidebar
selectedCategory={selectedCategory}
setSelectedCategory={setSelectedCategory}
/>
<Typography
calssName="copyright"
variant="body2"
sx={{ mt: 1.5, color: "#fff" }}
>
Copyright © 2022 YT Clone Made By Abdulrahman
</Typography>
</Box>
<Box p={2} sx={{ overflow: "auto", height: "90vh", flex: 2 }}>
<Typography variant="body2" sx={{ color: "white", mb: 2 }}>
<span
style={{ color: "#F31503", fontSize: "2rem", fontWeight: "bold" }}
>
<span style={{ color: "white" }}>{selectedCategory} </span>
Videos
</span>
</Typography>
<Videos videos={videos} />
</Box>
</Stack>
);
};
export default Feed;
注意:我的设计使用了materialUI(@mui/material([1]:https://i.stack.imgur.com/DqiPi.png
我也面临着类似的问题,现在它正在工作。
headers: {
"X-RapidAPI-Key": **process.env.REACT_APP_RAPID_API_KEY,**
"X-RapidAPI-Host": "youtube-v31.p.rapidapi.com",
},
您可以替换";process.env.RREACT_APP_RAPID_API_KEY";使用您在.env文件中设置的硬编码密钥。
例如像这个
headers: {
'X-RapidAPI-Key': `**f40bb900a8msh2675005f65b1b85p107114jsn879dac4fc413**`,
'X-RapidAPI-Host': 'youtube-v31.p.rapidapi.com',
}