2016-10-02 39 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問題,並希望避免第三方解!