我正在努力寻找一种使用JWT承载令牌向Microsoft Azure进行身份验证的解决方案,该令牌不使用简单的用户名和密码,而是使用客户端ID,客户端密码和其他两个参数。身份验证过程在Postman中运行时有效,但是当我使用Alamofire在Xcode中重新创建解决方案时,它会显示400错误。我想将Alamofire请求打印到控制台,以便我们可以看到Alamofire如何构造URL。
当我尝试将Alamofire的request类方法放入打印函数中时,它将无法编译,并且收到Xcode错误:
Value of tuple type '()' has no member 'validate'
这是我尝试打印到控制台的代码行:
print(Alamofire.request(authorizationURL, method: .post, parameters: parameters, encoding: URLEncoding(destination: .queryString), headers: headers))
答案 0 :(得分:0)
我意识到我将打印语句放在.validate语句之前的Alamofire请求块中。通过将打印语句移到请求语句之外,它可以工作。
import UIKit
import Alamofire
import SwiftyJSON
class Stackoverflow: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Stackoverflow.getAzureTokenOld()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc class func getAzureTokenOld(){
let authorizationPath: String = "https://login.microsoftonline.com/tenantID/oauth2/token"
if let authorizationURL: URL = URL.init(string: authorizationPath){
//do stuff with your authorization url
let parameters: [String: Any] = [
"grant_type" : "client_credentials",
"client_id" : "testID",
"client_secret" : "testSecret",
"resource" : "https://rest.media.azure.net"
]
let headers = [
"Content-Type": "application/x-www-form-urlencoded",
"Keep-Alive": "true"
]
Alamofire.request(authorizationURL, method: .post, parameters: parameters, encoding: URLEncoding(destination: .queryString), headers: headers)
.validate(statusCode: 200..<300)
.responseJSON { response in
switch response.result {
case .success:
print("Validation Successful")
let value = response.result.value
print("The valued response is: \(String(describing: value))")
case .failure(let error):
print(error.localizedDescription)
}
debugPrint("checking checking: \(response)")
}
print(Alamofire.request(authorizationURL, method: .post, parameters: parameters, encoding: URLEncoding(destination: .queryString), headers: headers))
}
}
}