首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误请求Alamofire + Basic Auth

错误请求Alamofire + Basic Auth
EN

Stack Overflow用户
提问于 2017-03-27 13:53:18
回答 2查看 979关注 0票数 1

我试图提出这个请求,但只返回错误401,请求正确吗?我要用基本的八月

代码语言:javascript
复制
    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的一部分吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-13 15:40:15

在您的代码中做一些小的更改,它就能工作了。

代码语言:javascript
复制
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

        }
}
票数 1
EN

Stack Overflow用户

发布于 2017-03-27 14:11:56

最好让阿拉莫火产生你的头。每https://github.com/Alamofire/Alamofire#authentication

编辑

替换这段代码

代码语言:javascript
复制
let credentialData = ("\(user):\(password)").data(using: String.Encoding.utf8)!
print(credentialData)
let base64Credentials = credentialData.base64EncodedString(options: [])
print(base64Credentials)
let headers: HTTPHeaders = ["Authorization":" Basic \(base64Credentials)"]

使用

代码语言:javascript
复制
var headers: HTTPHeaders = [:]

if let authorizationHeader = Request.authorizationHeader(user: user, password: password) {
    headers[authorizationHeader.key] = authorizationHeader.value
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43048342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档