我试图使用swift发布到使用密钥(MailGun)保护的API,但似乎我的密钥从未被使用,因为我收到了Forbidden 401错误(未经授权 - 未提供有效的API密钥) https://documentation.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")
在我看来,基本的身份验证凭据从未发送过?我怎样才能确定这些字段是&#34; user&#34;和&#34;密码&#34;?
答案 0 :(得分:0)
在验证我的标题似乎缺少标题的身份验证部分后,我能够通过大型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 Path在Keys.plist中作为名为mailgunAPIPath的字符串,其值为:
https://API:key-<my key>@api.mailgun.net/v3/<my domain>.com/messages?
希望这为任何遇到MailGun问题的人提供解决方案,并希望避免使用第三方解决方案!