我正在尝试快速发布到使用 key (MailGun)保护的 API,但似乎我的 key 从未被使用,因为我收到了根据 https://documentation.mailgun.com/api-intro.html#errors 的禁止 401 错误(未经授权 - 未提供有效的 API key )
我已经通过使用curl发帖验证了URL和 key 是否正确,但我无法弄清楚为什么这里没有使用我的 key 。我希望有人能够指出正确的方向,解释为什么验证不正确
我的代码是这样的,但我已将所有个人信息替换为<>:
// 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")
在我看来,基本的身份验证凭据从未发送过?我如何确定字段是“用户”和“密码”?
最佳答案
在验证我的 header 似乎缺少 header 的身份验证部分后,我能够通过大型 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 问题并希望避免使用第 3 方解决方案的其他人提供解决方案!
关于ios - 使用 swift 发布到安全 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39813277/