dynamic-media-cdn.tripadvisor.com/media/photo-o/17/0d/1e/c6/adina-apartment-hotel.jpg?w=900&h=-1&s=1:1
GET https://dynamic-media-cdn.tripadvisor.com/media/photo-o/17/0d/1e/c6/adina-apartment-hotel.jpg?w=900&h=-1&s=1 404 (Not Found)
requests.js:1
POST https://places.googleapis.com/v1/places:searchText 403 (Forbidden)
Error fetching place details: AxiosError {message: ‘Request failed with status code 403’, name: ‘AxiosError’, code: ‘ERR_BAD_REQUEST’, config: {…}, request: XMLHttpRequest, …}
this is the two files that are responsible for handling this:
useEffect(() => {
if (trip?.userSelection?.location?.label) {
getPlacePhoto();
}
}, [trip]);
const getPlacePhoto = async () => {
if (!trip?.userSelection?.location?.label) return;
setIsLoading(true);
try {
const data = {
textQuery: trip.userSelection.location.label,
languageCode: 'en'
};
const response = await getPlaceDetails(data);
console.log('Place Details Response:', response.data);
// Handle the response data to get the image URL
if (response.data?.places?.[0]?.photos?.[0]) {
const photoReference = response.data.places[0].photos[0].name;
// Set the photo URL
setPlaceImage(photoReference);
}
} catch (error) {
console.error('Error fetching place details:', error);
setPlaceImage(null); // Reset image on error
} finally {
setIsLoading(false);
}
};
and, this is for the api and axios:
import axios from 'axios';
const apiKey = import.meta.env.VITE_GOOGLE_PLACES_API_KEY;
export const getPlaceDetails = (data) => {
return axios.post(
'https://places.googleapis.com/v1/places:searchText',
data,
{
headers: {
'Content-Type': 'application/json',
'X-Goog-Api-Key': apiKey,
'X-Goog-FieldMask': 'places.displayName,places.formattedAddress,places.photos'
}
}
);
};
i am stuck on this from 2 hours. how to fix.
firstly, for testing, i was trying to console.log the place details and then try to use the images from google places api.
but i am getting the axios errors like 400, 401, 403, 404 etc.