AlamofireImage addAuthentication不添加授权标头

时间:2017-07-19 22:13:36

标签: authentication alamofireimage

我正在使用UIImageView.af_setImage扩展程序从需要身份验证的webdav服务器下载图像。

基于说...的文件。

  

如果图像需要来自UIImageView扩展的身份验证凭据,则可以按如下方式提供:

ImageDownloader.default.addAuthentication(user: "user", password: "password")

...我尝试过两种不同的方式添加身份验证:

ImageDownloader.default.addAuthentication(user: "user", password: "password")

当它不起作用时

UIImageView.af_sharedImageDownloader.addAuthentication(user: "user", password: "password")

但是,似乎都没有将Authorization标头发送到HTTP请求。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我没有使用addAuthentication(),而是通过安装我自己添加UIImageView.af_sharedImageDownloader标题的Authorization: Bearer yZyq6jYn78Ia9EOysUV8kQ解决了这个问题。

夫特

import Alamofire
import OAuthSwiftAlamofire
import OAuthSwift
import AlamofireImage

class MyNetwork {
    lazy var sessionManagerForImageDownloader: SessionManager = {
        let configuration: URLSessionConfiguration = ImageDownloader.defaultURLSessionConfiguration()
        let sessionManager = Alamofire.SessionManager(configuration: configuration)
        sessionManager.adapter = self.oauth2Swift.requestAdapter
        sessionManager.retrier = self.oauth2Swift.requestAdapter
        sessionManager.startRequestsImmediately = false
        return sessionManager
    }()

    func setup() {
        // Glue AlamofireImage with OAuthSwift
        // When `af_setImage()` is invoked, then the it will inserts
        // the header field "Authorization: Bearer yZyq6jYn78Ia9EOysUV8kQ"
        UIImageView.af_sharedImageDownloader = ImageDownloader(sessionManager: sessionManagerForImageDownloader)
    }

    lazy var oauth2Swift: OAuth2Swift = {
        return OAuth2Swift(
            consumerKey:    "clientID",
            consumerSecret: "clientSecret",
            authorizeUrl:   "https://example.com/login",
            accessTokenUrl: "https://example.com/login",
            responseType:   "code"
        )
    }()
}