I’m making a request to AccuWeather’s public API, first there was a problem with cors but I solved it, now it’s giving me an authorization error, what do I do?
import React from "react";
import { useState, useEffect } from 'react';
import './Citydata.css';
const Citydata = ({ cidade }) => {
const [location, setLocation] = useState(null);
const [error, setError] = useState(null);
const apiKey = 'ot0AvyzmQ1AkK3Z6c4BGzj7OAUwMRlIy'; // Substitua pela sua chave de API
useEffect(() => {
const getLocation = (city) => {
const url = `/locations/v1/cities/search?apikey=${apiKey}&q=${city}`;
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json(); // Chama o método json() corretamente
})
.then(data => {
setLocation(data);
setError(null);
})
.catch(err => {
console.error('Erro ao buscar localização:', err);
setError('Não foi possível buscar a localização.');
setLocation(null);
});
};
if (cidade) {
getLocation(cidade);
}
}, [cidade, apiKey]);
}
export default Citydata;
this is a get method API, what do I do ?
New contributor
Gustavo Lacerda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2