1

will some one please help me regarding this issue?

I am developing an app in swift 3 and I am trying to integrate google translate api in my iOS app but the rest api is responding with the following error:

["error": {
code = 403;
errors = (
{
domain = global;
message = "Requests from this ios client application <empty> are blocked.";
reason = forbidden;
}
);
message = "Requests from this ios client application <empty> are blocked.";
status = "PERMISSION_DENIED";
}]

3 Answers3

0

Have you followed: Implementing google translation api in swift 3 iOS ?

As he mentions you have to activate your account properly and register since it doesn't seem to be an free API to use anymore.

Community
  • 1
  • 1
Vollan
  • 1,887
  • 11
  • 26
  • I have properly set up the project. Enabled the api on the cloud developer console. Enabled billing and paid the cost of the translate api. But still getting the error. This error is very common on android devices as well. –  May 11 '17 at 17:33
0

I think this is not a problem of API free usage or anything, i have fixed the problem by changing "GET" method api call to "POST" method like this, you can try this -

open func translateTest(params: GoogleAITranslateParams, targetLanguage: String, callback:@escaping (_ translatedText:String) -> ()) {

    guard apiKey != "" else {
        return
    }

    var request = URLRequest(url: URL(string: "https://translation.googleapis.com/language/translate/v2?key=\(self.apiKey)")!)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue(Bundle.main.bundleIdentifier ?? "", forHTTPHeaderField: "X-Ios-Bundle-Identifier")

        let jsonRequest = [
            "q": params.text,
            "source": "en",
            "target": targetLanguage,
            "format": "text"
            ] as [String : Any]

        if let jsonData = try? JSONSerialization.data(withJSONObject: jsonRequest, options: .prettyPrinted) {
            request.httpBody = jsonData
            let task: URLSessionDataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
                guard error == nil else {
                    print("Something went wrong: \(String(describing: error?.localizedDescription))")
                    return
                }

                if let httpResponse = response as? HTTPURLResponse {

                    guard httpResponse.statusCode == 200 else {
                        if let data = data {
                            print("Response [\(httpResponse.statusCode)] - \(data)")
                        }
                        return
                    }

                    do {
                        if let data = data {
                            if let json = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {
                                if let jsonData = json["data"] as? [String : Any] {
                                    if let translations = jsonData["translations"] as? [NSDictionary] {
                                        if let translation = translations.first as? [String : Any] {
                                            if let translatedText = translation["translatedText"] as? String {
                                                callback(translatedText)
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } catch {
                        print("Serialization failed: \(error.localizedDescription)")
                    }
                }
            }

            task.resume()
        }
}
Rahul Singha Roy
  • 548
  • 1
  • 3
  • 13
0

Missing this line was the main reason to get blocked, since I restricted the API key to my iOS Bundle Identifier:

request.addValue(Bundle.main.bundleIdentifier ?? "", forHTTPHeaderField: "X-Ios-Bundle-Identifier")
wider-spider
  • 465
  • 4
  • 12