[英]Posting to a secure API with swift
我正在嘗試快速發布到使用密鑰(MailGun)保護的API,但是根據https:// documentation,由於收到禁止401錯誤(未授權-未提供有效的API密鑰),我的密鑰似乎從未使用過.mailgun.com / api-intro.html#errors
我已經通過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 <scheduler@<mg.mydomain.com>>&to: [bar@example.com,<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的響應:
curl -s --user 'api:key-<my personal key>' https://api.mailgun.net/v3/mg.<my domain>.com/messages -F from='Reservation Scheduler <scheduler@mg.<my domain>.com>' -F to=reservations@<my domain>.com -F subject='Curl Test' -F text='Test from terminal'
我嘗試像這樣明確輸入:
request.setValue("api", forHTTPHeaderField: "username")
request.setValue("key-<my key>", forHTTPHeaderField: "password")
在我看來,基本身份驗證憑據從未發送過? 如何確定字段為“ user”和“ password”?
驗證我的標頭似乎缺少標頭的身份驗證部分后,我能夠通過較大的HTTP響應使其正常工作。 我將完整路徑放入Keys.plist中,以便可以將我的代碼上傳到github並將一些參數分解為變量,以便稍后以編程方式設置它們。
// Email the FBO with desired information
// Parse our Keys.plist so we can use our path
var keys: NSDictionary?
if let path = NSBundle.mainBundle().pathForResource("Keys", ofType: "plist") {
keys = NSDictionary(contentsOfFile: path)
}
if let dict = keys {
// variablize our https path with API key, recipient and message text
let mailgunAPIPath = dict["mailgunAPIPath"] as? String
let emailRecipient = "bar@foo.com"
let emailMessage = "Testing%20email%20sender%20variables"
// Create a session and fill it with our request
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: NSURL(string: mailgunAPIPath! + "from=FBOGo%20Reservation%20%3Cscheduler@<my domain>.com%3E&to=reservations@<my domain>.com&to=\(emailRecipient)&subject=A%20New%20Reservation%21&text=\(emailMessage)")!)
// POST and report back with any errors and response codes
request.HTTPMethod = "POST"
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()
}
Mailgun路徑在Keys.plist中作為名為mailgunAPIPath的字符串,其值為:
https://API:key-<my key>@api.mailgun.net/v3/<my domain>.com/messages?
希望這能為其他與MailGun有問題並希望避免第三方解決方案的人提供解決方案!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.