【问题标题】:Alamofire 5 type 'Request' has no member 'authorizationHeader'Alamofire 5 类型 'Request' 没有成员 'authorizationHeader'
【发布时间】:2020-03-25 17:37:13
【问题描述】:

更新到 Alamofire 5 "Request.authorizationHeader(user: String, password: String)" 方法后显示错误。

Error:- Type 'Request' has no member 'authorizationHeader'

代码:

    // Safely unwrap token
    guard let safeHeader = Request.authorizationHeader(user: consumerKey!, password: consumerSecret!) else {
        return nil
    }

【问题讨论】:

  • 您使用的是哪个版本的 Alamofire?
  • 捆绑版本字符串(短)5.0.5
  • 您所说的“不工作”是什么意思,您是否有任何错误,或者您希望某些东西没有出现?如果其中任何一个,您可以发布错误或您期望的内容吗?
  • 更新问题请查看@denis_lor

标签: ios swift alamofire


【解决方案1】:

以前在Request.authorizationHeader(..) 下的内容现在在HTTPHeaders.authorization(..) 下,为了更好地解释它,我将把它的变化代码放在这里:

this commit 之前我们在 Request.swift 中:

/// Returns a base64 encoded basic authentication credential as an authorization header tuple.
///
/// - parameter user:     The user.
/// - parameter password: The password.
///
/// - returns: A tuple with Authorization header and credential value if encoding succeeds, `nil` otherwise.
open class func authorizationHeader(user: String, password: String) -> (key: String, value: String)? {
    guard let data = "\(user):\(password)".data(using: .utf8) else { return nil }
    let credential = data.base64EncodedString(options: [])
    return (key: "Authorization", value: "Basic \(credential)")
}

由于 Alamofire 5 中的 this commit 我们可以在 HTTPHeaders.swift 中找到它:

/// Returns a `Basic` `Authorization` header using the `username` and `password` provided.
///
/// - Parameters:
///   - username: The username of the header.
///   - password: The password of the header.
///
/// - Returns:    The header.
public static func authorization(username: String, password: String) -> HTTPHeader {
    let credential = Data("\(username):\(password)".utf8).base64EncodedString()

    return authorization("Basic \(credential)")
}

这意味着现在您应该能够通过以下方式做到这一点:

let headers: HTTPHeaders = [
        .authorization(username: consumerKey!, password: consumerSecret!),
        .accept("application/json")
    ]

【讨论】:

    猜你喜欢
    • 2020-01-01
    • 1970-01-01
    • 2015-04-17
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 2016-12-12
    • 2021-06-25
    相关资源
    最近更新 更多