2016-01-22 74 views
1

這裏是我的代碼:Alamofire的Swift 2.0:如何使用X-WSSE授權發送http頭文件?

> let uuid: CFUUIDRef = CFUUIDCreate(nil) 
    let nonce: CFStringRef = CFUUIDCreateString(nil, uuid) 
    let dateFormatter: NSDateFormatter = NSDateFormatter() 
    let timestamp = NSDate() 
    let secretKey = "DAqE6lBUgzxJRuR287GR" 
    let username = "us_demo003" 

    let formattedDate: String = dateFormatter.stringFromDate(timestamp) 


    let text = String((nonce as String)+formattedDate+secretKey) 

    let sha1 = text.sha1() 

    let passwordDigest = sha1.toBase64() 

    let headers = ["Username":username,"PasswordDigest": passwordDigest, "Nonce":(nonce as String), "Created":formattedDate] 

    let theUrlString = "url here" 

    manager.request(.GET, theUrlString, parameters: nil, encoding: ParameterEncoding.URL, headers: theHeaders).responseJSON { (result) -> Void in 
     print("BEGIN") 
     print("\n\n\n\n\n\n\n\nBEGIN\n\(result)\n\n\n\n\n\nEND") 
     print("STOP") 
    } 

我得到的結果如下:

SUCCESS: { 
data = ""; 
replyCode = 1; 
replyText = "No Authentication Header"; 

}

究竟如何發送認證頭與Alamofire?我檢查了github上的文檔。有些事情我做得不對。

感謝

+0

代碼中的'manager'如何?阿拉莫菲爾的一些包裝? –

+0

呀。經理是Alamofire.Manager.sharedInstance。它是一個包裝。 – Pleirosei

回答

1

相反,你要創建的Authorization頭,其中Authorization是字典鍵,WWSE profile=\"username_token\"是值。然後您還需要將X-WSSE標題密鑰設置爲UsernameToken Username="admin", PasswordDigest="buctlzbeVflrVCoEfTKB1mkltCI=", Nonce="ZmMzZDg4YzMzYzRmYjMxNQ==", Created="2014-03-22T15:24:49+00:00"。有關WSSE身份驗證

let token = "some generated token" 

let headers = [ 
    "Authorization": "WWSE profile=\"\(token)\"" 
    "X-WSSE": "UsernameToken Username=\"admin\", PasswordDigest=\"buctlzbeVflrVCoEfTKB1mkltCI=\", Nonce=\"ZmMzZDg4YzMzYzRmYjMxNQ==\", Created=\"2014-03-22T15:24:49+00:00\"" 
] 

更多信息,可以發現here

+0

謝謝cnoon!欣賞它。去給這個旋轉。 – Pleirosei