【发布时间】:2016-01-29 02:33:34
【问题描述】:
我正在尝试对需要相互身份验证的服务器进行 API 调用。 使用 NodeJS 和请求库,我可以使用以下代码访问 API
var keyFile = '/Users/username/Documents/Certificates/example-key.pem';
var certificateFile = '/Users/username/Documents/Certificates/cert.pem';
var options = {
uri: 'https://myserver.com/apiOne',
key: fs.readFileSync(keyFile),
cert: fs.readFileSync(certificateFile),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic ' + new Buffer(userId + ':' + password).toString('base64')
},
body: data //JSON body
};
request.postAsync(options)
.spread(function (response, body) {
res.status(200).json(JSON.parse(body));
})
.catch(function (err) {
res.status(400).send(err);
})
如何在 iOS 应用中使用相同的 API? 我正在使用以下代码,但服务器没有响应
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
if challenge.protectionSpace.authenticationMethod == "NSURLAuthenticationMethodServerTrust" {
let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust)
NodeJS 代码中的密钥和证书是什么意思?如何在 iOS 中传递它们?
【问题讨论】:
-
我没有将它用于推送通知。我只是想进行一个需要相互身份验证的 API 调用。
标签: ios node.js certificate ssl-certificate client-certificates