【问题标题】:Error request Alamofire + Basic Auth错误请求 Alamofire + Basic Auth
【发布时间】:2017-08-20 06:35:52
【问题描述】:

我正在尝试发出此请求,但只返回错误 401,请求是否正确?我必须使用基本身份验证

    var user = ""
    var password = ""

    user = textField.text!
    password = textField2.text!

    print(user)
    print(password)


    let credentialData = ("\(user):\(password)").data(using: String.Encoding.utf8)!
    print(credentialData)
    let base64Credentials = credentialData.base64EncodedString(options: [])
    print(base64Credentials)
    let headers: HTTPHeaders = ["Authorization":" Basic \(base64Credentials)"]

    print(headers)

    Alamofire.request("https://www.floratilemevidencia.com.br/wp-json/wp/v2/users/me", headers: headers)
        .validate().responseJSON { response in
            switch response.result {

            case .success:
                print("Validation Successful")
                let viewController: UIViewController = self.storyboard!.instantiateViewController(withIdentifier: "SideNavigationController")
                self.present(viewController, animated: true, completion: { _ in })

            case .failure(let error):
                print(error.localizedDescription)
                self.alertLabel.isHidden = false
            }
    }

它只返回错误 401。

它是 HTTPHeaders 的正确部分吗?

【问题讨论】:

  • 你找到答案了吗?

标签: ios swift3 request alamofire basic-authentication


【解决方案1】:

对你的代码做些小改动,它就会起作用。

var user = ""
var password = ""

user = textField.text!
password = textField2.text!

print(user)
print(password)


let credentialData = ("\(user):\(password)").data(using: String.Encoding.utf8)!
print(credentialData)

let base64Credentials = credentialData.base64EncodedString(options: [])
print(base64Credentials)

let url: String = "https://www.floratilemevidencia.com.br/wp-json/wp/v2/users/me"

var request = URLRequest(url:  NSURL(string: url)! as URL)
request.httpMethod = "GET"
request.setValue("Basic \(base64Credentials)", forHTTPHeaderField: "Authorization")

Alamofire.request(request)
    .validate().responseJSON { response in
        switch response.result {

        case .success:
            print("Validation Successful")
            // Your desired functionality

        case .failure(let error):
            print(error.localizedDescription)
            // Your desired functionality

        }
}

【讨论】:

    【解决方案2】:

    最好让 Alamofire 生成您的标题。每https://github.com/Alamofire/Alamofire#authentication

    编辑

    替换这段代码

    let credentialData = ("\(user):\(password)").data(using: String.Encoding.utf8)!
    print(credentialData)
    let base64Credentials = credentialData.base64EncodedString(options: [])
    print(base64Credentials)
    let headers: HTTPHeaders = ["Authorization":" Basic \(base64Credentials)"]
    

    var headers: HTTPHeaders = [:]
    
    if let authorizationHeader = Request.authorizationHeader(user: user, password: password) {
        headers[authorizationHeader.key] = authorizationHeader.value
    }
    

    【讨论】:

    • 它看起来如何,你能给我举个例子吗,我是 swift 新手
    • 我试过这个表格,它仍然返回错误401,会不会是服务器错误?
    • 我建议您尝试 Postman getpostman.com 并确认您的服务器正在返回正确的值。您是否有权访问您的服务器来查看日志?
    • 请使用 Charles 或类似的东西来比较您从 iOS 发出的请求与您从 Postman 发出的请求并发布详细信息。
    猜你喜欢
    • 2019-02-09
    • 2018-04-10
    • 2023-03-20
    • 2016-02-23
    • 2016-03-29
    • 2017-10-29
    • 2022-11-12
    • 2015-11-18
    • 1970-01-01
    相关资源
    最近更新 更多