iosxcoderestoauth

Swift (Xcode) REST API Connection using OAuth 1


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.

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.

Background:

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.

Company- FatSecret URL for API- platform.fatsecret.com/rest/server.api URL to FatSecret documentation (I have gone through this MANY times)- http:// {space} platform.fatsecret. {space }com/api/Default. {space} aspx?screen=rapiauth Parameters- Parameters { oauth_consumer_key - consumer_key (I have a consumer key) oauth_signature_method - "HMAC-SHA1" oauth_timestamp - 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 oauth_nonce - A randomly generated string for a request that can be combined with the timestamp to produce a unique value oauth_version - Must be "1.0" }

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.

Past 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()
                

The above code is what I used to establish the connection. I receive "success" in the console so I expanded my parameters.

    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()

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.

Again, any help would be incredibly appreciated.


Solution

  • This may be late but I have successfully managed to implement the FatSecret REST API and have created a small Xcode project that shows how I handled OAuth. The only calls that can be made are food.search and food.get. https://github.com/NicholasBellucci/FatSecretSwift