我正在尝试在我的iOS应用中设置Twilio可编程聊天,当我尝试获取访问令牌时,它返回Nil。我正在使用swift示例应用程序中提供的相同函数调用。
这是尝试获取字典
中存在的访问令牌的函数func fetchToken(params:[String:String], completion:@escaping (NSDictionary, NSError?) -> Void) {
if let filePath = Bundle.main.path(forResource: "Keys", ofType:"plist"),
let dictionary = NSDictionary(contentsOfFile:filePath) as? [String: AnyObject],
let tokenRequestUrl = dictionary["TokenRequestUrl"] as? String {
print(tokenRequestUrl)
var request = URLRequest(url: URL(string: tokenRequestUrl)!)
request.httpMethod = "POST"
let postString = self.postDataFrom(params: params)
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
completion(NSDictionary(), NSError(domain: "TWILIO", code: 1000, userInfo: [NSLocalizedDescriptionKey: "Incorrect return code for token request."]))
return
}
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String:Any]
print("json = \(json)")
completion(json as NSDictionary, error as NSError?)
} catch let error as NSError {
completion(NSDictionary(), error)
}
}
task.resume()
}
else {
let userInfo = [NSLocalizedDescriptionKey : "TokenRequestUrl Key is missing"]
let error = NSError(domain: "app", code: 404, userInfo: userInfo)
completion(NSDictionary(), error)
}
}
打印json
命令未执行,但当我尝试在Data
之前打印try
时,它返回2730字节。因此,null
作为字典传递,当我尝试获取访问令牌时,我最终得到nil
。
我刚尝试将数据打印为字符串。 HTML代码正在Data
对象