watsonbox / ios_google_places_autocomplete

Google Places address entry for iOS (Swift)
MIT License
269 stars 72 forks source link

Getting Error :- GooglePlaces API Error: REQUEST_DENIED #42

Open aanchalkalani opened 7 years ago

aanchalkalani commented 7 years ago

Hi,

While searching any thing getting error :- GooglePlaces API Error: REQUEST_DENIED

We added our API key properly and follow the below :-

Manual Simply copy GooglePlacesAutocomplete.swift and GooglePlacesAutocomplete.xib to your project. Note: Don't forget to add the PoweredByGoogle image to your xcassets.

Thanks,

RanjitKadam commented 7 years ago

@watsonbox , In swift 3 and ios 10 I am also getting the same error. Previously it used to work fine

Gladkov-Art commented 7 years ago

The same problem for me.

Gladkov-Art commented 7 years ago

It seems that I have tracked down the problem. It was related to escaping of optional query parameters. Here is the solution (I have rewritten the origin functions):

  fileprivate class func query(_ parameters: [String: AnyObject]) -> String {
    var components: [(String, String)] = []
    for key in Array(parameters.keys).sorted(by: <) {
        if let value = parameters[key] as? String {
            let escapedValue = escape(value)
            components += [(escape(key), escapedValue)]
        }
    }
    return (components.map{"\($0)=\($1)"} as [String]).joined(separator: "&")
  }

  fileprivate class func escape(_ string: String) -> String {
    var escapedCharacters = CharacterSet.urlQueryAllowed
    escapedCharacters.remove(charactersIn: ":/?&=;+!@#$()',*")
    return string.addingPercentEncoding(withAllowedCharacters: escapedCharacters) ?? ""
  }