Bearer token issue in Swift - returning 401, Headers

I need some help in either our server API setup or the client setup in Swift…

I have an accessToken - JWT.io - looks good. It is the accessToken. Send it our test api - http://EXAMPLE_IP/api/private and receive a 401, Headers server response. We have followed the samples, troubleshoot, searched here and stack overflow, but we can’t seem to resolve the issue.

Client-side code:

Auth0
.webAuth()
.scope(“openid profile update:users scan:attendees”)
.audience(“api.EXAMPLE_URL.edu”)
.start {
switch $0 {
case .failure(let error):
print(“Error: (error)”)
case .success(let credentials):
guard let accessToken = credentials.accessToken else { return }
print(accessToken)
let headers = [“Authorization”: "Bearer " + accessToken]

                let request = NSMutableURLRequest(url: NSURL(string: "http://EXAMPLE_IP/api/private")! as URL,
                                                  cachePolicy: .useProtocolCachePolicy,
                                                  timeoutInterval: 10.0)
                request.httpMethod = "GET"
                //request.addValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
                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()

The Api lives behind a firewall, but the dashboard has the listing of api.andersonuniversity.edu, and my coworker set it up following the samples. We have altered the audience and the scopes, but we can’t seem to move past this. Unfortunately, we are hitting a critical mass deadline. I could really use some help here.

Thanks in advance

Hi @nkennedy617,

It looks like you need to authorize your app to use your api. This can be done by going to the dashboard → APIs → Your API → Machine to Machine → *toggle on your application.

Let me know if this fixes it.

Thanks,
Dan

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.