Search code examples
swiftxcodegoogle-cloud-platformgoogle-cloud-functions

Swift Google Autocomplete API: "Internal Error, code: 403"


I'm using the Google Places SDK for iOS (version 9.2.0) to implement an autocomplete feature in my Swift app. The autocomplete search works fine, showing a list of recommended places. However, when I select a place, I encounter the following error:

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

enter image description here

Google Places SDK Version: 9.2.0

Steps Taken:

  • Verified that my API key is valid and enabled in the Google Cloud Console.
  • Ensured that the Places API and billing are active for my project.
  • Checked that there are no restrictions on my API key (currently no restrictions applied for testing purposes).
  • Examined the quotas for the Places API, and usage is well below limits.
  • Rechecked the API endpoint and parameters for correctness.

Here is the code I am using, which always falls into the didFailAutocompleteWithError:

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()
        }
    }
}

I have also tested the API using a curl request in Postman, and it works perfectly there. However, in Xcode, the issue persists. I have tried the following:

Changing the network.

  • Verifying the API key usage across the entire app.
  • Double-checking settings in the Google Cloud Console.
  • Everything appears to be configured correctly. What could I be missing?

Solution

  • Answering this as a community wiki. As stated in this github

    Google Places SDK Version 9.x seems to require the new Places API (new) which you have to activate for your API key (in case it's restricted)