gpt4 book ai didi

swift - 我有谷歌云翻译 API key ,但它仍然显示响应 [403] - 355 字节

转载 作者:行者123 更新时间:2023-11-30 11:44:23 25 4
gpt4 key购买 nike

我正在使用 RoGoogleTranslate 并使用计费帐户 Google 翻译 API key ,但它仍然无法正常工作,我从 github 下载演示

最佳答案

尝试这个“POST”方法功能 -

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

关于swift - 我有谷歌云翻译 API key ,但它仍然显示响应 [403] - 355 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com