由于 API 密钥无效(格式错误或丢失),操作失败

Joa*_*uin 5 google-maps ios google-places-api swift

使用适用于 IOS 的 Google 代码,描述如下:自动完成

\n\n

用户在搜索框中输入第一个字符时,会发生错误:

\n\n
Error:  The operation couldn\xe2\x80\x99t be completed. Operation failed due to an invalid (malformed or missing) API key. See the developer\'s guide (https://developers.google.com/places/ios-sdk/start) for information on creating and using an API key.\ndespite that calls to the API were successful:\nGMS Services Key Valid:  true\nGMS Services SDK version:  3.3.0\nGMS Places Key Valid:  true\nGMS Places SDK version:  3.3.0\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是应用程序的跟踪:

\n\n
2019-08-17 16:46:52.056351-0400 GPDemo[345:15310] [DYMTLInitPlatform] platform initialization successful\nEntering AppDelegate.swift now...\nSending API key to Google Services and Places Client\nGMS Services Key Valid:  true\nGMS Services SDK version:  3.3.0\nGMS Places Key Valid:  true\nGMS Places SDK version:  3.3.0\nLeaving AppDelegate.swift now...\nwarning: could not execute support code to read Objective-C class data in the process. This may reduce the quality of type information available.\nEntering viewDidLoad now...\nLeaving viewDidLoad now...\n2019-08-17 16:51:54.281297-0400 GPDemo[345:15082] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles\n2019-08-17 16:51:54.283666-0400 GPDemo[345:15082] [MC] Reading from public effective user settings.\nEntering didUpdateAutocompletePredictions function now...\nLeaving didUpdateAnyAutocompletePredictions function now...\n2019-08-17 16:51:55.829602-0400 GPDemo[345:15082] [framework] CUIThemeStore: No theme registered with id=0\nEntering didRequestAutocompletePredictions function now...\nLeaving didRequestAutocompletePredictions function now...\nEntering didUpdateAutocompletePredictions function now...\nLeaving didUpdateAnyAutocompletePredictions function now...\nEntering resultsContoller didFailAutocomplete function now...\nError:  The operation couldn\xe2\x80\x99t be completed. Operation failed due to an invalid (malformed or missing) API key. See the developer\'s guide (https://developers.google.com/places/ios-sdk/start) for information on creating and using an API key.\nLeaving resultsController didFailAutocomplete function now...\n2019-08-17 16:54:13.293721-0400 GPDemo[345:15869] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C1.1:2][0x105e40a60] get output frames failed, state 8196\n2019-08-17 16:54:13.297185-0400 GPDemo[345:15869] [BoringSSL] nw_protocol_boringssl_get_output_frames(1301) [C1.1:2][0x105e40a60] get output frames failed, state 8196\n2019-08-17 16:54:13.299238-0400 GPDemo[345:15869] TIC Read Status [1:0x0]: 1:57\n2019-08-17 16:54:13.300271-0400 GPDemo[345:15869] TIC Read Status [1:0x0]: 1:57\n
Run Code Online (Sandbox Code Playgroud)\n\n

任何帮助表示赞赏。

\n\n

使用相同的密钥访问 Map API 效果很好!

\n\n
//  AppDelegate.swift\n//  GPDemo\n//\n\n\n\n    import UIKit\n    import GoogleMaps\n    import GooglePlaces\n\n    @UIApplicationMain\n    class AppDelegate: UIResponder, UIApplicationDelegate {\n\n        var window: UIWindow?\n\n\n        func application(_ application: UIApplication, \n        didFinishLaunchingWithOptions launchOptions: \n        [UIApplication.LaunchOptionsKey: Any]?) -> Bool {\n        // Override point for customization after application launch.\n\n        print("Entering AppDelegate.swift now...")\n\n        print("Sending API key to Google Services and Places Client")\n\n        //// Maps API\n        // JpDimoi "kiquoa-ios-places" key... 8/9/19\n        let GMSServicesKeyValid = \n        GMSServices.provideAPIKey("MY_API_KEY")\n        // JpDimoi "kiquoa-ios" key...\n\n        let GMSServicesSDKVer = GMSServices.sdkVersion()\n        print("GMS Services Key Valid: ", GMSServicesKeyValid)\n        print("GMS Services SDK version: ", GMSServicesSDKVer)\n\n        //// Places API\n        // JpDimoi "kiquoa-ios-places" key... 8/9/19\n        let GMSPlacesKeyValid =      \n\n\n        GMSPlacesClient.provideAPIKey(\n         "MY_API_KEY")\n        // JpDimoi "kiquoa-ios" key...\n\n        let GMSPlacesSDKVer = GMSPlacesClient.sdkVersion()\n        print("GMS Places Key Valid: ", GMSPlacesKeyValid)\n        print("GMS Places SDK version: ", GMSPlacesSDKVer)\n\n        print("Leaving AppDelegate.s`enter code here`wift now...")\n\n        return true\n        }\n\n        func applicationWillResignActive(_ application: UIApplication) {\n            // Sent when the application is about to move from active to \n            // inactive state. This can occur for certain types of temporary \n            // interruptions (such as an incoming phone call or SMS message) \n            // or when the user quits the application and it begins the \n            // transition to the background state.\n            // Use this method to pause ongoing tasks, disable timers, and \n            // invalidate graphics rendering callbacks. Games should use this \n            // method to pause the game.\n        }\n\n        func applicationDidEnterBackground(_ application: UIApplication) {\n            // Use this method to release shared resources, save user data, \n            // invalidate timers, and store enough application state \n    //information to \n            // restore your application to its current state in case it is \n    //terminated \n            // later.\n            // If your application supports background execution, this method \n            // is called instead of applicationWillTerminate: when the user \n    // quits.\n        }\n\n        func applicationWillEnterForeground(_ application: UIApplication) {\n            // Called as part of the transition from the background to the \n            // active state; here you can undo many of the changes made on \n    // entering \n            // the background.\n        }\n\n        func applicationDidBecomeActive(_ application: UIApplication) {\n            // Restart any tasks that were paused (or not yet started) while \n            // the application was inactive. If the application was \n            // previously in the  \n            // background, optionally refresh the user interface.\n        }\n\n        func applicationWillTerminate(_ application: UIApplication) {\n            // Called when the application is about to terminate. Save data \n            // if appropriate. See also applicationDidEnterBackground:.\n        }\n\n\n    }\n\n    //  GPDemo ViewController.swift\n    //\n    //  Exact copy of code for results view controller\n    //  when adding a searchBar to the top of the view\n    //  Ref: https://developers.google.com/places/ios-sdk/autocomplete\n\n    import UIKit\n    import GooglePlaces\n\n    class ViewController: UIViewController {\n\n        var resultsViewController: GMSAutocompleteResultsViewController?\n        var searchController: UISearchController?\n        var resultView: UITextView?\n\n        override func viewDidLoad() {\n\n            print("Entering viewDidLoad now...")\n            super.viewDidLoad()\n\n            resultsViewController = GMSAutocompleteResultsViewController()\n            resultsViewController?.delegate = self\n\n            searchController = UISearchController(searchResultsController: \n            resultsViewController)\n            searchController?.searchResultsUpdater = resultsViewController\n\n            let subView = UIView(frame: CGRect(x: 0, y: 65.0, width: 350.0, \n            height: 45.0))\n\n            subView.addSubview((searchController?.searchBar)!)\n            view.addSubview(subView)\n            searchController?.searchBar.sizeToFit()\n            searchController?.hidesNavigationBarDuringPresentation = false\n\n            // When UISearchController presents the results view, present it \n            // in\n            // this view controller, not one further up the chain.\n            definesPresentationContext = true\n\n            print("Leaving viewDidLoad now...")\n        }\n    }\n\n    // Handle the user\'s selection.\n    extension ViewController: GMSAutocompleteResultsViewControllerDelegate {\n        func resultsController(_ resultsController: \n         GMSAutocompleteResultsViewController,\n         didAutocompleteWith place: GMSPlace) {\n            searchController?.isActive = false\n            print("Entering resultsController didAutocompleteWith place \n                  function now...")\n            // Do something with the selected place.\n            //print("Place name: \\(place.name)")\n            print("Place address: \\(String(describing:     \n            place.formattedAddress))")\n            //print("Place attributions: \\(place.attributions)")\n            print("Leaving resultsController didAutocompleteWith place \n                 function now...")\n        }\n\n        func resultsController(_ resultsController: \n            GMSAutocompleteResultsViewController,\n            didFailAutocompleteWithError error: Error){\n            // TODO: handle the error.\n            print("Entering resultsContoller didFailAutocomplete function \n                  now...")\n            print("Error: ", error.localizedDescription)\n            print("Leaving resultsController didFailAutocomplete function \n                  now...")\n        }\n\n        // Turn the network activity indicator on and off again.\n        func didRequestAutocompletePredictions(forResultsController \n            resultsController: GMSAutocompleteResultsViewController) {\n            print("Entering didRequestAutocompletePredictions function \n                  now...")\n            UIApplication.shared.isNetworkActivityIndicatorVisible = true\n            print("Leaving didRequestAutocompletePredictions function \n                  now...")\n        }\n\n        func didUpdateAutocompletePredictions(forResultsController \n            resultsController: GMSAutocompleteResultsViewController) {\n            print("Entering didUpdateAutocompletePredictions function \n                  now...")\n            UIApplication.shared.isNetworkActivityIndicatorVisible = false\n            print("Leaving didUpdateAnyAutocompletePredictions function \n                  now...")\n\n        }\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n

预计提案清单将完成演讲。

\n

eva*_*van 0

您遇到的此错误很可能是由您的云项目配置引起的,而不是由于您自己的代码实现引起的,特别是考虑到您在使用 Google 的示例代码时遇到相同的错误(如果您使用来自一个有效的项目)。

首先,无论您当前是否处于开发阶段,都必须在您的项目上启用计费功能,Maps API 才能正常工作。

其次,旧的适用于 IOS 的 Places SDK 已被弃用。要使用适用于 IOS 的新 Places SDK,您需要启用并添加到 API 密钥的 API 限制的 API 是Places API

我建议您仔细阅读 Google 的入门指南,正确设置您的项目和 API 密钥,以便与适用于 IOS 的新 Places SDK 一起使用: https:
//developers.google.com/maps/gmp-get-started
https://developers .google.com/places/ios-sdk/start

希望这可以帮助。