我試圖發佈到使用swift密鑰(MailGun)加密的API,但是看起來我的密鑰從未被利用,因爲我收到Forbidden 401錯誤(未授權 - 沒有有效的API密鑰提供)根據https://documentation.mailgun.com/api-intro.html#errors使用swift發佈到安全的API
我已通過發帖使用curl驗證了URL和密鑰是正確的,但我無法弄清楚爲什麼我的密鑰沒有在這裏使用。我希望有人能在正確的方向,爲什麼這一點是沒有正確驗證
我的代碼是這樣,但我已經取代所有的個人信息與<>:
// Email the FBO with desired information
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.mailgun.net/v3/<My Domain>/messages")!)
request.HTTPMethod = "POST"
let data = "from: Excited User <[email protected]<mg.mydomain.com>>&to: [[email protected],<my email>]&subject:Hello&text:Testinggsome Mailgun awesomness!"
request.HTTPBody = data.dataUsingEncoding(NSASCIIStringEncoding)
request.setValue("key-<my key>", forHTTPHeaderField: "api")
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
if let error = error {
print(error)
}
if let response = response {
print("url = \(response.URL!)")
print("response = \(response)")
let httpResponse = response as! NSHTTPURLResponse
print("response code = \(httpResponse.statusCode)")
}
})
task.resume()
更新:
闖入它幾個小時,我仍然無法繞過它。也許我不完全確定你的意思?我可以用順利拿到,捲曲的迴應:
curl -s --user 'api:key-<my personal key>' https://api.mailgun.net/v3/mg.<my domain>.com/messages -F from='Reservation Scheduler <[email protected]<my domain>.com>' -F [email protected]<my domain>.com -F subject='Curl Test' -F text='Test from terminal'
我試圖明確輸入它像這樣:
request.setValue("api", forHTTPHeaderField: "username")
request.setValue("key-<my key>", forHTTPHeaderField: "password")
它看起來像我的基本身份驗證憑據不會發送?我怎樣才能確定這些字段是「用戶」和「密碼」?
您誤解了文檔。您需要提供「api」作爲用戶名和您的密鑰作爲基本身份驗證標頭的密碼 – Paulw11
昨晚和今天上午花了幾個小時,我仍然無法擺脫困境。發佈了更新。它可能是憑證不實際發送?看着捲曲它看起來像api - >關鍵給我。我試圖明確設置用戶名和密碼。 沮喪,休息一下。 –
我試圖打印會話,所以我可以看到請求,但我只收到一個八進制回來。是否有可能正確打印請求,以便我可以調試發送到錯誤位置/根本不發送的內容? –