2

I've been doing some tests connecting to an OAuth service using Postman. Everything works great and I can see the JSON response. However, the code that Postman generates doesn't include any of the tokens.

How would I do this in Swift? I also don't want to use any 3rd party libraries, as I want to learn how to do this.

Here is the code that Postman generates. There is nothing about how to generate the token.

import Foundation

let headers = [
  "cache-control": "no-cache",
  "postman-token": "31761212-1492-2111-aa2d-da54b8f1983b"
]

let request = NSMutableURLRequest(url: NSURL(string: "https://xxxdev.service-now.com/api/now/table/sc_request")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Thanks

Jeshua Lacock
  • 5,730
  • 1
  • 28
  • 58
Paul S.
  • 1,342
  • 4
  • 22
  • 43
  • The postman token is nothing to do with OAuth. See https://stackoverflow.com/questions/36883046/what-is-the-postman-token-in-generated-code-from-postman. Obtaining an OAuth token would require you to implement then OAuth protocol, which isn’t trivial which is why you would typically use a 3rd party framework – Paulw11 Nov 03 '17 at 20:05

0 Answers0