I am trying to call the Google Maps Distance Matrix API with React and Node.js, using Axios for the API request, to get the time it will take for a user to get from their current location to an airport. The API is enabled in Google Cloud Console, but whenever I call this API, it always gives returns in console the message, “REQUEST DENIED”.
I have CORS enabled, so I do not anticipate that being the issue. I have set up the OAuth from Google Cloud Console, placed a time break in between calls of the API (which I removed from my current code), and enabled billing for the API, but the same response still shows. I saw that there is a post the post that mentions using the API key that Google used in their demo, but I was not able to access the page that they have linked. I have my API restrictions, the response from the console, and the code for the request below. Any help would be greatly appreciated!
The output from console from the API request
The API’s restrictions from Google Cloud Console
Code:
export const fetchDistanceMatrix = createAsyncThunk(
'distances/fetchDistanceMatrix',
async ({ orLat, orLong, lat, long }, thunkAPI) => {
try {
const testUrl = "https://api.distancematrix.ai/maps/api/distancematrix/json?origins=" + orLat + "%2C" + orLong + "&destinations=" + lat + "%2C" + long + "&units=imperial" + "&key=AIzaSyCRIyTFyAUw8qvpQ37j25ZhtOKFng1cDxY";
const response = await axios.get(testUrl);
if (response.data.status === "OK")
return response.data.rows[0].elements[0].duration;
else
return thunkAPI.rejectWithValue(response.data.status)
} catch (error) {
return thunkAPI.rejectWithValue({ error: error.message });
}
}
);
1