GPT助手调用Google Directions API失败:密钥无效及服务状态异常求助
问题描述
我是物流公司的新手用户,想创建GPT助手辅助工作,通过调用Google Directions API计算两点间距离。已通过GPT Action Assistant编写调用API的OpenAPI配置:
info: title: Google Maps Directions API Custom Wrapper (With API Key) description: API to retrieve driving directions using Google Maps Platform. version: 1.0.0 servers: - url: https://maps.googleapis.com/maps/api/directions description: Google Maps Directions API server paths: /json: get: operationId: getDirectionsFromIncubatoioToDestination summary: Get driving directions from Forlì to a specified destination. parameters: - name: destination in: query required: true description: Destination address. schema: type: string - name: origin in: query required: true description: Fixed origin address (Forlì). schema: type: string default: Forlì - name: key in: query required: true description: Your Google Maps Platform API key. schema: type: string default: YOUR_ACTUAL_API_KEY responses: "200": description: Successful response with directions. content: application/json: schema: type: object properties: status: type: string routes: type: array items: type: object properties: summary: type: string legs: type: array items: type: object properties: distance: type: object properties: text: type: string value: type: integer duration: type: object properties: text: type: string value: type: integer end_address: type: string start_address: type: string steps: type: array items: type: object
操作Google Cloud时遇到两个问题:
- 创建项目并尝试启用Directions API时,页面显示服务已禁用,点击启用后通知栏仍提示服务已禁用
- 创建API密钥并填入GPT Builder认证区域后,测试收到报错:
{ "domain": "maps.googleapis.com", "method": "get", "path": "/json", "operation": "getDirectionsFromIncubatoioToDestination", "operation_hash": "eec1196d2cb02d763554336f2992883cda86a118", "is_consequential": false, "params": { "destination": "VIA DELLA SEGA,6 - FONTIGO DI SERNAGAGLIA DELLA BATTAGLIA (TV)", "origin": "Incubatoio Faentino Forlì", "key": "YOUR_API_KEY" } } [debug] Response received { "response_data": { "error_message": "The provided API key is invalid. ", "routes": [], "status": "REQUEST_DENIED" }, "status_code": 200, "action_id": "g-b95e704cd746d14f88df8964141c60edf3412748" }
注:已将配置中的YOUR_ACTUAL_API_KEY替换为真实密钥Aizaxxxx...,此前求助Gemini及GPT未解决问题。
排查与解决方案
针对API启用异常问题
- 确认项目一致性:检查操作的Google Cloud项目和后续创建API密钥的项目是否为同一个,避免跨项目操作导致的状态不同步
- 强制刷新控制台:使用
Ctrl+F5刷新API库页面,或更换浏览器查看,排除控制台缓存导致的显示异常 - 检查账单状态:进入Google Cloud账单页面,确认已绑定有效的支付方式(即使在免费额度内,未绑定支付方式会导致API无法正常启用),且无未结清的账单
- 查看操作日志:进入「API和服务」>「仪表板」>「活动」,查看启用Directions API的操作记录,是否有明确的报错信息提示失败原因
针对API密钥无效问题
- 验证密钥正确性:检查密钥是否完整复制,无多余空格、大小写错误(Google API密钥区分大小写)
- 检查密钥API限制:进入「API和服务」>「凭据」,找到目标密钥:
- 若设置为「限制密钥」,确认已将「Directions API」添加到允许的API列表中
- 测试阶段可暂时切换为「不限制密钥」,排除限制规则导致的访问拒绝
- 检查IP限制:如果密钥设置了IP白名单,确认GPT Action的请求IP在允许范围内(GPT Builder的请求IP为动态,测试时可暂时移除IP限制)
- 本地直接测试密钥:用curl命令在本地验证密钥有效性,替换为你的真实密钥:
curl "https://maps.googleapis.com/maps/api/directions/json?origin=Incubatoio+Faentino+Forlì&destination=VIA+DELLA+SEGA%2C6+-+FONTIGO+DI+SERNAGAGLIA+DELLA+BATTAGLIA+(TV)&key=你的真实密钥"
若返回仍提示密钥无效,说明密钥本身存在问题,尝试重新创建新的API密钥测试
- 确认GPT Action配置:检查OpenAPI配置中
key参数的default值是否已替换为真实密钥,同时确认GPT Builder认证区域的密钥填写正确(部分场景下,GPT Action会优先使用认证区域的密钥)
额外建议
- 等待API生效延迟:Google Cloud启用API后可能需要5-10分钟的生效时间,启用后不要立即测试,等待一段时间再尝试
- 查看错误报告:进入「API和服务」>「错误报告」,查看是否有更详细的错误日志,帮助定位具体问题
内容的提问来源于stack exchange,提问作者Mike G. Poli
相关产品推荐
相关产品推荐

