【发布时间】:2017-12-22 02:48:33
【问题描述】:
我通过调用 translate api 收到此错误消息“来自此 ios 客户端应用程序 \u003cempty\u003e 的请求被阻止”,我已在网络上设置为 ios 应用程序和我的捆绑包 ID。请帮忙
let TRANSLATE_API = "https://translation.googleapis.com/language/translate/v2"
let GOOGLE_CLOUD_API_KEY = "<API_KEY>"
let urlParams:[String : Any] = [
"target": target,
"q": textToTranslate,
"key": GOOGLE_CLOUD_API_KEY,
"source": source]
let bundleIdentifier = Bundle.main.bundleIdentifier!
let headers:[String : String] = ["Content-Type": "application/json", "X-Ios-Bundle-Identifier": bundleIdentifier]
// Fetch Request
let urlString = TRANSLATE_API
// Fetch Request
Alamofire.request(urlString, parameters: urlParams)
.validate()
.responseJSON { (response) in
switch(response.result) {
case .success(_):
if response.result.error != nil {
completion(false, response.result.error! as! String)
}
if let json = response.result.value as? [String: Any] {
if let data = json["data"] as? [String: Any] {
if let translations = data["translations"] as? [[String:Any]] {
let translatedTextDict = translations[0]
if let result = translatedTextDict["translatedText"] as? String {
completion(true, result)
}
}
}
}
break
case .failure(_):
completion(false, (response.result.error?.localizedDescription)!)
break
}
}
【问题讨论】:
标签: ios swift google-translate