swift - 使用Alamofire时,元组类型的值没有成员验证意味着什么?

标签 swift alamofire

我正在研究一种使用 JWT 不记名 token 向 Microsoft Azure 进行身份验证的解决方案,该解决方案不使用简单的用户名和密码,而是使用客户端 ID、客户端 key 和其他两个参数。身份验证过程在 Postman 中运行时有效,但是当我使用 Alamofire 在 Xcode 中重新创建解决方案时,它显示 400 错误。我想将 Alamofire 请求打印到控制台,以便我们可以看到 Alamofire 如何构建 URL。

当我尝试将 Alamofire 的请求类方法放入打印函数中时,它不会编译,并且收到 Xcode 错误:

Value of tuple type '()' has no member 'validate'

这是我尝试打印到控制台的代码行:

print(Alamofire.request(authorizationURL, method: .post, parameters: parameters, encoding: URLEncoding(destination: .queryString), headers: headers))

最佳答案

我意识到我将 print 语句放置在 Alamofire 请求 block 中的 .validate 语句之前。通过将打印语句移动到请求语句之外,它就可以工作。

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))
        }

    }
}

关于swift - 使用Alamofire时,元组类型的值没有成员验证意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51123930/

相关文章:

ios - 核心蓝牙 : didWriteValueFor() is not called after writeValue()

arrays - 使用数组中的值将 Section 添加到 tableView

ios - 将静态库目标转换为 Xcode 项目中的框架目标

swift - Alamofire 请求链接 - 无法调用非函数类型的值 'HTTPURLResponse'

ios - 如何在 AlamofireXMLRPC 请求上设置 HTTP header 字段?

ios - 从右到左支持而不是一些 View 及其 subview

swift - 如何在 NSView 子类中绘制两行文本? (macOS 系统托盘)

ios - Alamofire 和 Swift 3 的更新

ios - Alamofire 4 请求返回 NSArray,无法弄清楚如何在 Swift 3 中使用 SwiftyJSON 进行解析

ios - 在 Alamofire 2.0 中使用客户端证书