2016-10-02 52 views
0

我试图发布到使用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") 

它看起来像我的基本身份验证凭据不会发送?我怎样才能确定这些字段是“用户”和“密码”?

+0

您误解了文档。您需要提供“api”作为用户名和您的密钥作为基本身份验证标头的密码 – Paulw11

+0

昨晚和今天上午花了几个小时,我仍然无法摆脱困境。发布了更新。它可能是凭证不实际发送?看着卷曲它看起来像api - >关键给我。我试图明确设置用户名和密码。 沮丧,休息一下。 –

+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 = "[email protected]" 
    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%[email protected]<my domain>.com%3E&[email protected]<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问题,并希望避免第三方解!