You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Cloud Functions调用带Referer限制的Places API密钥遭拒求助

解决Firebase Cloud Functions调用Google Places API的密钥限制问题

你遇到的报错本质是Referer(HTTP引用)限制仅适用于浏览器发起的客户端请求,而Firebase Cloud Functions属于服务器端运行环境,发起请求时不会携带符合Referer限制规则的请求头,因此会被Google API拒绝。

具体解决方案

方案一:调整现有密钥的限制规则

  • 进入Google Cloud控制台的API密钥管理页面,找到目标密钥。
  • 移除当前的「HTTP引用(网站)」限制,改为添加「IP地址(服务器端)」限制。
  • 将Firebase Cloud Functions的出站IP地址添加到允许列表:如果使用默认环境,可添加Google Cloud Functions的全局出站IP段;若使用VPC网络,添加对应VPC的出站IP即可。

方案二:创建独立的服务器端专用密钥

  • 在API密钥管理页面新建一个密钥,专门用于Firebase Cloud Functions的调用。
  • 给新密钥仅设置「IP地址(服务器端)」限制,不要设置任何Referer相关限制。
  • 在Cloud Functions代码中替换为这个新密钥,原有带Referer限制的密钥继续用于客户端网站请求。

你的请求代码示例

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)

报错信息

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

内容的提问来源于stack exchange,提问作者AlienDecoy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 18:35:20