[英]Is there a way to overcome 401 unauthorized error in React, while trying to access data from Rapidapi.com?
I am facing the problem of unauthorized 401. Done everything mentally possible, so posting it here with code and result pics.我正面临未经授权的 401 问题。尽了一切可能,因此将其与代码和结果图片一起发布在这里。 The data when accessed like this retrieves data without problem.
像这样访问时的数据可以毫无问题地检索数据。
var axios = require("axios").default; var options = { method: 'GET', url: 'https://coinranking1.p.rapidapi.com/stats', headers: { 'x-rapidapi-host': 'coinranking1.p.rapidapi.com', 'x-rapidapi-key': 'key' } }; axios.request(options).then(function (response) { console.log(response.data); }).catch(function (error) { console.error(error); });
But when I try to access it via the code, it does not.但是当我尝试通过代码访问它时,它没有。 please have a look at the code.
请看一下代码。
index.js索引.js
import React from "react"; import ReactDom from "react-dom"; import { BrowserRouter as Router } from "react-router-dom"; import { Provider } from "react-redux"; import App from "./App"; import store from "./app/store"; import "antd/dist/antd.css"; ReactDom.render( <Router> <Provider store={store}> <App /> </Provider> </Router>, document.getElementById("root") );
CryptoApi.js CryptoApi.js
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react"; const cryptoApiHeaders = { "x-rapidapi-host": "coinranking1.p.rapidapi.com", "x-rapidapi-key": "key", }; const baseUrl = "https://coinranking1.p.rapidapi.com"; const createRequest = (url) => ({ url, header: cryptoApiHeaders }); export const cryptoApi = createApi({ reducerPath: "cryptoApi", baseQuery: fetchBaseQuery({ baseUrl }), endpoints: (builder) => ({ getCryptos: builder.query({ query: () => createRequest("/coins"), }), }), }); export const { useGetCryptosQuery } = cryptoApi;
store.js商店.js
import { configureStore } from "@reduxjs/toolkit"; import { cryptoApi } from "../services/cryptoApi"; export default configureStore({ reducer: { [cryptoApi.reducerPath]: cryptoApi.reducer, }, });
Homepage.jsx首页.jsx
import React from "react"; import millify from "millify"; import { Typography, Row, Col, Statistic } from "antd"; import { Link } from "react-router-dom"; import { useGetCryptosQuery } from "../services/cryptoApi"; const { Title } = Typography; const Homepage = () => { const { data, isFetching } = useGetCryptosQuery(); console.log(data); return ( <> <Title level={2} className="heading"> Global Crypto Stats </Title> <Row> <Col span={12}> <Statistic title="Total Cryptocurrencies" value="5" /> <Statistic title="Total Exchanges" value="5" /> <Statistic title="Total Market Cap" value="5" /> <Statistic title="Total 24h Voulume" value="5" /> <Statistic title="Total Markets" value="5" /> </Col> </Row> </> ); }; export default Homepage;
Thank you.谢谢你。
Quite interesting, I believe you're already subscribed to API. Generally, a 410 code indicates a lack of authentication.挺有意思的,我相信你已经订阅了API。一般情况下,410代码表示没有认证。
You can try this in CryptoApi.js file你可以在 CryptoApi.js 文件中试试这个
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react"; const cryptoApiHeaders = { "x-rapidapi-host": "coinranking1.p.rapidapi.com", "x-rapidapi-key": "key", }; const baseUrl = "https://coinranking1.p.rapidapi.com/stats"; const createRequest = (url) => ({ url, header: cryptoApiHeaders }); export const cryptoApi = createApi({ reducerPath: "cryptoApi", baseQuery: fetchBaseQuery({ baseUrl }), endpoints: (builder) => ({ getCryptos: builder.query({ query: () => createRequest("/coins"), }), }), }); export const { useGetCryptosQuery } = cryptoApi;
If it still doesn't work, you can always reach out to the RapidAPI support team at support@rapidapi.com如果它仍然不起作用,您可以随时通过 support@rapidapi.com 联系 RapidAPI 支持团队
On your CryptoApi.js file, there's a typo在你的 CryptoApi.js 文件中,有一个错字
const createRequest = (url) => ({ url, header : cryptoApiHeaders }); const createRequest = (url) => ({ url, header : cryptoApiHeaders });
should be 'headers: ...'应该是“标题:...”
That should fix the 401 issue.这应该可以解决 401 问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.