title: Swift (Xcode) REST API Connection using OAuth 1
body: <p>Quick background, I am extremely new in this realm. I am aware that this type of question has been asked before and answered successfully. The issue that I am experiencing is caused by the inability to wrap my head around the process of establishing the connection. I have spent hours (into days) searching for the answer and I am still unsuccessful. This has become my "white whale" so to speak. </p>
  
  <p>I am using Xcode 9 with Swift version 4. Many of the answer I come across use Objective-C and I cannot mix and match. So I would like to UNDERSTAND why I am unable to connect and the correct process to connect so I can write the code with the understanding of what I am doing. Lastly, I have signed up (and completed) a few paid Udemy courses to try and learn the process correctly. I have been able to connect to API sources but OAuth 1 is tripping me up. Any constructive help would be incredibly appreciated.</p>
  
  <p>Background:</p>
  
  <p>I am attempting to connect to the Fat Secret database. I would like to connect a search bar to the food.search functionality and also the food.get for another search bar. </p>
  
  <p>Company- <em>FatSecret</em>
  URL for API- <em>platform.fatsecret.com/rest/server.api</em>
  URL to FatSecret documentation (I have gone through this MANY times)- <em>http:// {space} platform.fatsecret. {space }com/api/Default. {space} aspx?screen=rapiauth</em>
  Parameters- Parameters {
  <em>oauth_consumer_key</em> - <em>consumer_key (I have a consumer key)</em>
  <em>oauth_signature_method</em> - <em>"HMAC-SHA1"</em>
  <em>oauth_timestamp</em> - <em>The date and time, expressed in the number of seconds since January 1, 1970 00:00:00 GMT. The timestamp value must be a positive integer and must be equal or greater than the timestamp used in previous requests</em>
  <em>oauth_nonce</em> - A randomly generated string for a request that can be combined with the timestamp to produce a unique value
  <em>oauth_version</em> - Must be "1.0"
  }</p>
  
  <p>As I previously stated, the answer to my question is displayed above. I understand that part but I do not understand how to incorporate it into my code. </p>
  
  <p>Past code-</p>
  
  <pre><code>        let url = URL(string: "I am unable to post more than 2 links due to my rep so I put {space} in the above url to circumvent the error. I used the listed url from the parameters")!
  
  
          let task = URLSession.shared.dataTask(with: url) {
              (data, response, error) in
  
             if error != nil { print("success")
  
     }    task.resume()
  </code></pre>
  
  <p>The above code is what I used to establish the connection. I receive "success" in the console so I expanded my parameters. </p>
  
  <pre><code>    let url = URL(string: "I am unable to post more than 2 links due to my rep so I put {space} in the above url to circumvent the error. I used the listed url from the parameters")!
  
  
                          let task = URLSession.shared.dataTask(with: url) {
                              (data, response, error) in
  
                             if error != nil { print(error)
  
          if let urlContent = data {
  
                              do {
  
                             let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
  
  print(jsonResult)
  
      } catch {
  
  
  
  
                 }    task.resume()
  </code></pre>
  
  <p>The above code produces nothing in the console. I believe (sorry for my ignorance) that the reason I am not getting a response is because I am not sending any authorization in the request, nor am I am sending in the correct encoding. I imagine that I can create the parameters by var/let statements and then call on those statements but I am not able to see the way to do that. I could likely also store all of my connection information in a different swift file or class and call on that when I need to access data. This base signature is required with every request. I have to imagine that best practice would be setting it up that way but again, I can't visualization the process. It becomes a trial and error process that results in incredible frustration. </p>
  
  <p>Again, any help would be incredibly appreciated. I apologize for the length of this post. Thank you for taking the time to read this post. </p>