简体   繁体   English

Alamofire 5.0+ MultipartFormData 上传不记名令牌

[英]Alamofire 5.0+ MultipartFormData upload bearer token

I have gotten everything to work on the alamofire multipart form data upload but adding the bearer token.我已经完成了 alamofire 多部分表单数据上传的所有工作,但添加了不记名令牌。 We use Oauth 2.0 and our files need the token to authenticate in our systems.我们使用 Oauth 2.0 并且我们的文件需要令牌在我们的系统中进行身份验证。 I have only found a way to pass the username and password which doesnt work in our current system.我只找到了一种方法来传递在我们当前系统中不起作用的用户名和密码。 Is there a way to pass the bearer token to the php?有没有办法将不记名令牌传递给 php?

   AF.upload(multipartFormData: { multipartFormData in

    for (key, value) in parameters {
        multipartFormData.append(value.data(using: .utf8)!, withName: key)
    }

if let jpegData = image.jpegData(compressionQuality: 1.0) {
        multipartFormData.append(jpegData, withName: "file", fileName: "image", mimeType: "image/jpeg")
    }
}, to: "https:website" )
.uploadProgress{ progress in
//print("Upload Progress: \(self.progress.fractionCompleted)")
}
.response { response in
if response.response?.statusCode == 200 {
    print("OK. Done")
    print((NSString(data: response.data!, encoding: 
String.Encoding.utf8.rawValue)! as String))
}else if response.response?.statusCode == 406{
    print("error")
    DispatchQueue.main.sync{
        let alert = UIAlertController(title: "Error", message: 
"Person has not been set up.", preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "close", style: .default, handler: { action in
             DispatchQueue.main.async{
                self.progressUiView.isHidden = true
                self.dismiss(animated: true, completion: nil)
            }
        }))

        self.present(alert, animated: true)
    }
}else{
    print(response.response?.statusCode)
}
}

You need to add the Authorization token to the headers .您需要将 Authorization 令牌添加到headers Here's how:就是这样:

let token = "your_token_here"
AF.upload(multipartFormData: { multipartFormData in
    //...
}, to: "https:website", headers: ["Authorization": "Bearer \(token)"])
    .response { response in
    //...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM