1

I recently restricted an api key to accept requests from specific websites, but I use the same api key from Firebase Cloud Functions for Places search.

What url do I add for the cloud functions request?

Url is built like this

const urlFindPlaceFromText = "https://maps.googleapis.com/maps/api/place/findplacefromtext";
const fields = "formatted_address,name,geometry,icon,rating,price_level,place_id";
const location = "point:$latitude, $longitude";
const url = `${urlFindPlaceFromText}/json?input=${searchString}&inputtype=textquery&language=en&fields=${fields}&locationbias=${location}&key=${apiKey}`;
const placesRequest = await axios.get(url)

Response:

data: {
>      candidates: [],
>      error_message: 'API keys with referer restrictions cannot be used with this API.',
>      status: 'REQUEST_DENIED'
>    }

enter image description here

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
AlienDecoy
  • 135
  • 1
  • 11
  • A separate key should work for server side apps. Have you checked [this answer](https://stackoverflow.com/questions/42167695/api-key-browser-api-keys-cannot-have-referer-restrictions-when-used-with-this-ap)? – Dharmaraj Dec 04 '22 at 06:04
  • HTTP referrer is the domain name of the website. Where is the API Key used (browser or backend server)? https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer – John Hanley Dec 04 '22 at 07:23
  • Please check my recommendations below and let me know if those were helpful – Vaidehi Jamankar Dec 13 '22 at 07:36

1 Answers1

1

The error you are seeing looks like you are making the API call server side. Because you have placed a referrer restriction on your API key, it will be limited to executing on the browser with the web service APIs. As mentioned in the comments above,you may create a separate key to use server-side. You can change your restriction from a browser restriction to a server restriction by using IP addresses to restrict access, instead of browser referrers.
Check this APIs FAQ on switching key type to a server restricted key

Also check these similar example for more information:

Vaidehi Jamankar
  • 1,232
  • 1
  • 2
  • 10