0

Trying to create a simple oAuth1 query in Swift and not getting anywhere. I have tried following the signature spec to no avail. Granted, this is somewhat topical to accessing data in GravityForms but the answer shouldn't be GF-specific. The GF docs are not detailed.

I am trying to use the OhhAuth library which seems to be closest, but I haven't gotten there. No matter what I've tried I'm getting 401s

The simple function looks like this:

func getOrders() {
    
    let cc = (key: "****", secret: "***")
    var req = URLRequest(url: URL(string: "https://savannahsconecompany.com/wp-json/gf/v2/entries")!)

    req.oAuthSign(method: "GET", consumerCredentials: cc)
    let task = URLSession(configuration: .ephemeral).dataTask(with: req) { (data, response, error) in
        
        if let error = error {
            print(error)
        }
        else if let data = data {
            print(String(data: data, encoding: .utf8) ?? "Does not look like a utf8 response :(")
        }
    }
    task.resume()
    
}

The error result I get is: {"code":"rest_forbidden","message":"Sorry, you are not allowed to do that.","data":{"status":401}}

I don't need to use OhhAuth. It just looked simple. What I really need to do is create a conforming REST URL that will work. I can get Postman to work. The Swift port of its code is:

var semaphore = DispatchSemaphore (value: 0)
var request = URLRequest(url: URL(string: "https://savannahsconecompany.com/wp- 
     json/gf/v2/entriesoauth_consumer_key=myConsumerKey&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1595676418&oauth_nonce=bIX9CEnrsql&oauth_version=1.0&oauth_signature=aA+8BBLh4W6njyL1m6lsh04Zyts=")!,timeoutInterval: Double.infinity)
request.httpMethod = "GET"

let task = URLSession.shared.dataTask(with: request) { data, response, error in 
  guard let data = data else {
    print(String(describing: error))
    return
  }
  print(String(data: data, encoding: .utf8)!)
  semaphore.signal()
}

task.resume()
semaphore.wait()

This confirms that it's something in the signature construction. I can easily replicate all of this in my own call, but I can't seem to get the oAuth1 signature right.

The spec for the Gravity Forms v2 API Authentication doc (for Oauth1) is here.

I've also looked this and this.

Any help is greatly appreciated. Not an expert Swift dev but should be able to solve this issue.

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
S Graham
  • 162
  • 13

0 Answers0