Reputation: 3952
I'm working towards a solution to authenticate with Microsoft Azure using JWT bearer tokens which doesn't use a simple username and password but rather a client id, client secret, and two other parameters. The authentication process works when ran in Postman however when I recreate the solution in Xcode using Alamofire, it displays a 400 error. I'd like to print out to the console, the Alamofire request so that we can see how Alamofire is structuring the URL.
When I attempt to put Alamofire's request class method inside a print function, it won't compile and I receive the Xcode error:
Value of tuple type '()' has no member 'validate'
Here is the line of code where I'm attempting to print to the console:
print(Alamofire.request(authorizationURL, method: .post, parameters: parameters, encoding: URLEncoding(destination: .queryString), headers: headers))
Upvotes: 0
Views: 391
Reputation: 3952
I realized I placed the print statement within the Alamofire request block before my .validate statement. By moving the print statement to outside of the request statement it works.
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))
}
}
}
Upvotes: 0