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

Swift集成Google Places SDK 9.2.0选地点时403内部错误排查

Swift中使用Google Places SDK 9.2.0选择地点时出现403内部错误

问题描述

搜索地点自动补全推荐列表可正常显示,但选择地点时触发以下错误:

Autocomplete error: Internal Error, code: 403, info: Error
Domain=com.google.HTTPStatus Code=403 "Internal Error"
UserInfo={NSLocalizedDescription=Internal Error}

Google Places SDK版本:9.2.0

已完成的排查步骤

  • 验证API密钥在Google Cloud Console中有效且已启用
  • 确认项目的Places API和计费功能已激活
  • API密钥未设置任何限制(测试阶段)
  • Places API配额使用量远低于限制
  • 核对API端点及参数正确性
  • Postman测试curl请求功能正常
  • 更换网络环境、检查API密钥在应用中的使用情况、重新核对Cloud Console配置

相关代码

import Foundation
import GooglePlaces
import SwiftUI

struct GooglePlacesSearchView: UIViewControllerRepresentable {
    @Binding var selectedPlace: GMSPlace?
    @Binding var locationName: String
    @Binding var lat: Double
    @Binding var long: Double
    @Environment(\.presentationMode) var presentationMode

    func makeUIViewController(context: Context) -> UIViewController {
        let autocompleteController = GMSAutocompleteViewController()
        autocompleteController.delegate = context.coordinator
        return autocompleteController
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        // Update any properties or UI elements here if needed.
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject, GMSAutocompleteViewControllerDelegate {
        var parent: GooglePlacesSearchView

        init(_ parent: GooglePlacesSearchView) {
            self.parent = parent
        }

        func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
            parent.selectedPlace = place
            parent.presentationMode.wrappedValue.dismiss()
            parent.locationName = place.name ?? ""
            parent.lat = place.coordinate.latitude
            parent.long = place.coordinate.longitude
        }

        func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
            debugPrint("Autocomplete error: \(error.localizedDescription), code: \(error._code), info: \(error)")
        }

        func wasCancelled(_ viewController: GMSAutocompleteViewController) {
            parent.presentationMode.wrappedValue.dismiss()
        }
    }
}

可能的解决方案

1. 检查Places API的完整权限配置

自动补全列表加载仅需基础的Autocomplete API权限,但选择地点后获取完整GMSPlace数据时,需要确保Cloud Console中已启用**Places API (New)**核心服务。若仅开启Autocomplete API而未启用核心Places API,会导致详情接口调用失败。

2. 再次核对API密钥的应用限制

即使测试阶段未设置限制,也需确认Cloud Console的API密钥页面中“应用限制”是否为空,或填写的iOS Bundle ID与Xcode项目中的完全一致(注意大小写、拼写)。误添加的不匹配Bundle ID限制会直接触发403。

3. 清理Xcode缓存并重新安装SDK

  • 执行Product > Clean Build Folder清理编译缓存,再通过Organizer > Projects > 你的项目 > Delete Derived Data删除衍生数据
  • 若使用CocoaPods,执行pod deintegrate && pod install重新安装GooglePlaces SDK,避免依赖文件损坏

4. 检查Info.plist权限配置

确保Info.plist中添加了NSLocationWhenInUseUsageDescription(位置使用权限描述),部分版本的Places SDK会通过权限验证应用合法性,缺失该配置可能导致异常。

5. 启用Google Maps SDK for iOS

部分版本的Places SDK依赖Maps SDK的底层服务,即使不显示地图,也需在Cloud Console中启用Maps SDK for iOS,否则获取地点详情时可能触发权限错误。

6. 确认计费账户状态

检查Cloud Console计费页面,确保账户无欠费,Places API的计费状态正常。即使配额未超限,计费账户异常也可能导致403错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 08:52:09