Alamofire 2 不允许 SSL 连接 Instagram API
Posted
技术标签:
【中文标题】Alamofire 2 不允许 SSL 连接 Instagram API【英文标题】:Alamofire 2 not allowing SSL connection with Instagram API 【发布时间】:2015-10-03 23:39:01 【问题描述】:我将 Alamofire 1 与 Swift 1.2 一起使用,并且以下请求正在运行:
let instagramUrl = "https://api.instagram.com/v1/media/search?omittedKey=omittedValue"
Alamofire.request(.GET, instagramUrl).responseJSON (_, _, JSON) in ...
当我更新到 Swift 2 和 Alamofire 2 时,我收到了这个错误:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo=NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, NSUnderlyingError=0x7d0580f0 Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo=_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamErrorCodeKey=-9802, _kCFStreamErrorDomainKey=3, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7b7ef7d0>, kCFStreamPropertySSLPeerCertificates=<CFArray 0x7b6805f0 [0x1e64098]>type = immutable, count = 2, values = (
0 : <cert(0x7b7eee80) s: *.instagram.com i: DigiCert High Assurance CA-3>
1 : <cert(0x7b7ef1f0) s: DigiCert High Assurance CA-3 i: DigiCert High Assurance EV Root CA>
), _kCFStreamErrorCodeKey=-9802, NSErrorFailingURLStringKey=https://api.instagram.com/v1/media/search?omittedKey=omittedValue, NSErrorPeerCertificateChainKey=<CFArray 0x7b6805f0 [0x1e64098]>type = immutable, count = 2, values = (
0 : <cert(0x7b7eee80) s: *.instagram.com i: DigiCert High Assurance CA-3>
1 : <cert(0x7b7ef1f0) s: DigiCert High Assurance CA-3 i: DigiCert High Assurance EV Root CA>
), NSErrorClientCertificateStateKey=0, NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7b7ef7d0>, NSErrorFailingURLKey=https://api.instagram.com/v1/media/search?omittedKey=omittedValue
所以我尝试使用它:
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"api.instagram.com": .DisableEvaluation,
"*.instagram.com": .DisableEvaluation
]
let manager = Manager(
configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
manager.request(.GET, instagramUrl).responseJSON (_, _, JSON) in
我得到了这个错误:
Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=NSErrorFailingURLKey=https://api.instagram.com/v1/media/search?omittedKey=omittedValue, NSErrorFailingURLStringKey=https://api.instagram.com/v1/media/search?omittedKey=omittedValue, NSLocalizedDescription=cancelled
我尝试的最后一种方法是使用 CustomServerTrustPolicyManager,但我不知道在 func serverTrustPolicyForHost(host: String)
上添加什么
class CustomServerTrustPolicyManager: ServerTrustPolicyManager
override func serverTrustPolicyForHost(host: String) -> ServerTrustPolicy?
var policy: ServerTrustPolicy?
policy = ServerTrustPolicy.DisableEvaluation
return policy
并替换了
let manager = Manager(
configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
到
let manager = Manager(
configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
serverTrustPolicyManager: CustomServerTrustPolicyManager(policies: serverTrustPolicies)
)
但我得到了同样的错误:
Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo=NSErrorFailingURLKey=https://api.instagram.com/v1/media/search?omittedKey=omittedValue, NSErrorFailingURLStringKey=https://api.instagram.com/v1/media/search?omittedKey=omittedValue, NSLocalizedDescription=cancelled
我该怎么做才能让它发挥作用?最好是正确的方式。
谢谢。
【问题讨论】:
【参考方案1】:您的 manager
实例正在被释放,因为您没有维护对它的引用,或者您在查询字符串中传递的凭据无效。
【讨论】:
以上是关于Alamofire 2 不允许 SSL 连接 Instagram API的主要内容,如果未能解决你的问题,请参考以下文章
由于 TIC SSL 信任错误,无法使用 Alamofire 发出请求