API 请求 RAPID API 给出 StatusCode: 401,

API Request to RAPID API gives StatusCode: 401,

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';

const cryptoApiHeaders = {
  'x-rapidapi-key': 'coinranking1.p.rapidapi.com',
  'x-rapidapi-host': '935af67341msh072d3f07b71a1f8p1cb0b5jsn44961d90a001',
};

const baseUrl = 'https://coinranking1.p.rapidapi.com';

const createRequest = (url) => ({ url, headers: cryptoApiHeaders });

export const cryptoApi = createApi({
  reducerPath: 'cryptoApi',
  baseQuery: fetchBaseQuery({ baseUrl }),
  endpoints: (builder) => ({
    getCryptos: builder.query({
      query: () => createRequest('/coins'),
    }),
  }),
});

export const { useGetCryptosQuery } = cryptoApi;

** 我尝试从 RapidApi 连接 api 但我一直收到 401 未经授权。 我自己想不通:3请有人帮助我 **

你的headers应该是这个

const cryptoApiHeaders = {
  'x-rapidapi-host': 'coinranking1.p.rapidapi.com',
  'x-rapidapi-key': '935af67341msh072d3f07b71a1f8p1cb0b5jsn44961d90a001',
};

只需传递 API 密钥并以另一种方式托管。像这样

'x-rapidapi-key': '935af67341msh072d3f07b71a1f8p1cb0b5jsn44961d90a001',
'x-rapidapi-host': 'coinranking1.p.rapidapi.com',

P.S。 API 密钥是一个可能被滥用的敏感凭据。您刚刚公开了 API 密钥。我建议您删除此 API 密钥并在 RapidAPI

Developer Dashboard 为您生成一个新密钥