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

Google Place API请求被拒求助:Python调用API授权失败

调用Google Places API请求被拒的排查解决

问题描述

在笔记本的Spyder中使用Python代码调用Google Places API,下载悉尼地区邮局的名称和坐标时,遇到请求被拒的问题。已完成以下操作:

  • 创建Google Cloud项目并启用相关API(Places API、Geocoding API、Maps JavaScript API、Geolocation API、Maps Embed API)
  • 确认API无使用限制
  • 创建并关联了计费账户

使用的代码如下:

import googlemaps
import time
gmaps = googlemaps.Client(key= '....') # 替换为自己的API密钥

coordinates = []

query = "Post Office, Sydney"

try:
    response = gmaps.places(query=query)
    if response["status"] == "OK":  # 检查请求是否成功
        places = response.get("results", [])

        for place in places:
            try:
                location = place["geometry"]["location"]
                lat, lng = location["lat"], location["lng"]
                place_details = gmaps.place(place_id=place["place_id"])
                types = place_details.get("result", {}).get("types", [])  # 获取地点类型

                # 确保至少有三个类型字段
                place_type1 = types[0] if len(types) > 0 else "N/A"
                place_type2 = types[1] if len(types) > 1 else "N/A"
                place_type3 = types[2] if len(types) > 2 else "N/A"

                coordinates.append([place["name"], place_type1, place_type2, place_type3, lat, lng])  # 添加邮局名称
                time.sleep(0.5) # 减少请求间隔,避免触发限制

            except Exception as e:
                print(f"处理地点时出错: {e}")

    else:
        print(f"错误: API请求失败,状态码 {response['status']}")  # 添加错误提示
        print(response)  # 打印完整响应用于调试

except Exception as e:
    print(f"发起API请求时出错: {e}")

# 打印结果(测试用)
for coord in coordinates:
    print(coord)

收到的错误信息:

Error making API request: REQUEST_DENIED (This API project is not authorized to use this API.)
翻译:发起API请求时出错:请求被拒绝(此API项目未被授权使用该API)

排查与解决方法

  • 确认目标API是否正确启用:gmaps.places()调用的是「Places Text Search API」,gmaps.place()调用的是「Places Details API」,这两个都属于Google Places API的子服务。去Google Cloud控制台的API库,搜索「Places API」,确认该API的状态为「已启用」,避免误启用了其他无关API。
  • 检查API密钥的权限限制:
    1. 进入Google Cloud控制台的「API密钥」页面,找到当前使用的密钥。
    2. 查看「API限制」设置:如果选择了「限制密钥」,需确保列表中包含「Places API」;若不确定,可暂时设置为「不限制密钥」测试是否解决问题。
    3. 查看「应用限制」:如果设置了IP地址限制,确认当前笔记本的公网IP已添加到允许列表中,或者暂时关闭IP限制测试。
  • 验证计费账户状态:进入Cloud Billing页面,确认账户处于正常状态,无欠费、未验证或暂停情况。即使关联了账户,未完成身份验证也可能导致API请求被拒。
  • 绕过第三方库直接测试:使用requests库直接发送HTTP请求,排除googlemaps库的版本或配置问题:
    import requests
    
    API_KEY = "你的API密钥"
    # 测试Text Search API
    text_search_url = f"https://maps.googleapis.com/maps/api/place/textsearch/json?query=Post+Office%2C+Sydney&key={API_KEY}"
    text_response = requests.get(text_search_url)
    print("Text Search响应:", text_response.json())
    
    # 如果Text Search成功,再测试Details API(用返回的place_id)
    if text_response.json().get("status") == "OK":
        place_id = text_response.json()["results"][0]["place_id"]
        details_url = f"https://maps.googleapis.com/maps/api/place/details/json?place_id={place_id}&key={API_KEY}"
        details_response = requests.get(details_url)
        print("Details响应:", details_response.json())
    
    查看返回的JSON中的error_message字段,通常会给出更具体的拒绝原因。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 19:16:03