Problem getting products through sylius API using iOS


#1

Currently I’m trying to consume sylius api using the domain demo.sylius.com, I’ve authenticated properly and got an access token with no problem but when it comes to passing the access token to the authorization header I ran into a problem with response { error = "access_denied"; "error_description" = "OAuth2 authentication required"; } although the api works fine through postman , here’s my sample code

func getTotalProducts(page:String, completionHandler: @escaping (_ totalProducts: TotalProducts) -> Void){
    let urlPath=APIManager.baseURL+"api/v2/products"
    let headers = ["Content-Type":"application/json","Authorization":"Bearer \(token)","X-Authorization":"Bearer SampleToken","Auth":"Bearer SampleToken","auth":"Bearer SampleToken"]
    var request = URLRequest(url:  NSURL(string: urlPath)! as URL)
    
    // Your request method type Get or post etc according to your requirement
    request.httpMethod = "GET"
    
    request.setValue("Bearer SampleToken", forHTTPHeaderField: "Authorization")
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("Bearer SampleToken", forHTTPHeaderField: "X-Authorization")
    request.setValue("Bearer SampleToken", forHTTPHeaderField: "auth")
    
    Alamofire.request(request)
        .responseJSON { response in
            switch(response.result){
            case .success(let value):
                print(value)
                let totalProducts = Mapper<TotalProducts>().map(JSON: value as! [String : Any])!
                completionHandler(totalProducts)
                break
            case .failure(let error):
                print("something went wrong \(error.localizedDescription)")
                break
            }
        }.session.finishTasksAndInvalidate()
}

#2

I remember an issue where it was the server (Apache) stripping the headers needed for authentication. Are you able to see the request in Symfony Profiler? I’d check if it contains the desired headers.

EDIT: I did not notice you are using demo.sylius.com. :slight_smile: Can you try it with local Sylius installation and see if it is any different?


#3

I will try to see symphony profiler to see the results, but if so why the request succeeds through postman?
@pjedrzejewski