简体   繁体   English

在尝试从 Rapidapi.com 访问数据时,有没有办法克服 React 中的 401 未授权错误?

[英]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;

Errors in console:控制台错误: 安慰

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.

相关问题 来自 DRF 的 React Native 401 未经授权的错误 - React Native 401 Unauthorized Error From DRF 从React请求API数据时,为什么会收到401未经授权的错误? - Why am I receiving a 401 Unauthorized error when requesting API data from React? 从 ReactJS 调用(使用)REST URL 时出现 401(未经授权的错误) - 401(unauthorized error ) while invoking(consuming) a REST URL from ReactJS 从React返回的环回注销401(未授权) - Loopback Logout from React returns 401 (Unauthorized) React 配置文件页面,如何在尝试从后端获取 JSON 数据时避免“GET http://localhost:3001/users/profile 401 (Unauthorized)” - React profile page, how to avoid 'GET http://localhost:3001/users/profile 401 (Unauthorized)' when trying to get JSON data from back end 反应 axios 401 未授权 - React axios 401 unauthorized 401 未经授权的反应 axios - 401 Unauthorized react axios Axios 错误 401(未经授权)尝试获取 api 时出错 - Axios Error 401 (Unauthorized) Error when trying to fetch api 使用部分前端时处理来自 rails api 的 401 未经授权错误(反应) - Handling the 401 Unauthorized error from rails api when using a partial frontend (react) React 应用程序错误“服务器响应错误 401(未经授权)” - React Application error "The server responded with error 401 (unauthorized) "
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM