Uber Invalid OAuth 2.0 credentials provided Uber Authentication In ios Swift

后端 未结 3 493
野性不改
野性不改 2020-12-16 04:18

I\'m implementing the Uber\'s Request Endpoint in my iOS (Swift) App. The Request API/Endpoint requires the user authentication with the app, here is the doc.

For th

相关标签:
3条回答
  • 2020-12-16 04:49

    Finally I have done it :)

    I changed the method like below and it Worked

    func callRequestAPI(url:String){
    
        var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        var session = NSURLSession(configuration: configuration)
    
        let params:[String: AnyObject] = [
            "product_id" : selectedUberProductId,
            "start_latitude" : start_lat,
            "start_longitude" : start_lng,
            "end_latitude" : end_lat,
            "end_longitude" : end_lng]
    
    
    
        let request = appDelegate.oauth.request(forURL: NSURL(string:url)!)
        request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
        request.HTTPMethod = "POST"
        var err: NSError?
        request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.allZeros, error: &err)
    
        let task = session.dataTaskWithRequest(request) {
            data, response, error in
    
            if let httpResponse = response as? NSHTTPURLResponse {
                if httpResponse.statusCode != 202 {
                    println("response was not 202: \(response)")
    
                    return
                }
            }
            if (error != nil) {
                println("error submitting request: \(error)")
                return
            }
    
            // handle the data of the successful response here
            var result = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: nil) as! NSDictionary
    
    
            println(result)
    
            if let request_id: String = result["request_id"] as? String{
    
                println(request_id)
            }
    
            if let driver: String = result["driver"] as? String{
    
                println(driver)
            }
    
            if let eta: Int = result["eta"] as? Int{
    
                println(eta)
            }
    
            if let location: String = result["location"] as? String{
    
                println(location)
            }
    
    
            if let status: String = result["status"] as? String{
    
                println(status)
            }
    
    
            if let surge_multiplier: Int = result["surge_multiplier"] as? Int{
    
                println(surge_multiplier)
            }
    
            if let vehicle: String = result["vehicle"] as? String{
    
                println(vehicle)
            }
    
        }
    
        task.resume()
    
    }
    

    here is the Response I Got, Parsing is also given in my above method

    {
      driver = "<null>";
      eta = 15;
      location = "<null>";
      "request_id" = "ea39493d-b718-429f-8710-00a34dcdaa93";
      status = processing;
      "surge_multiplier" = 1;
      vehicle = "<null>";
    }
    

    Enjoy

    0 讨论(0)
  • 2020-12-16 04:51

    To use the token just follow step 5 of the instructions in the OAuth2 library, like you did before you started to try to sign it yourself a second time. The request has already been signed and has the Bearer token set up, there is nothing left to do for you:

    let url = NSURL(string: "https://api.uber.com/v1/products?latitude=37.7759792&longitude=-122.41823")
    let req = appDelegate.oauth.request(forURL: url)
    
    // customize your request, if needed. E.g. for POST:
    req.HTTPMethod = "POST"
    
    // send the request
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(req) { data, response, error in
        if nil != error {
            // something went wrong
        }
        else {
            // check the response and the data
            // you have just received data with an OAuth2-signed request!
        }
    }
    task.resume()
    
    0 讨论(0)
  • 2020-12-16 05:00

    Updated for Swift 2. I used the same setup and library for oauth that Qadir describes in his question. I updated his request to work in Swift 2. Hope this helps others.

    uberRequest:

        let params:[String:AnyObject] = [
            "product_id" : uberProduct,
            "start_latitude" : userLat,
            "start_longitude" : userLng,
            "end_latitude" : barLat,
            "end_longitude" : barLng]
    
        let urlPath = "https://sandbox-api.uber.com/v1/requests"
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        var session = NSURLSession(configuration: configuration)
    
        guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint");return }
    
        let request = appDelegate.oauth.request(forURL: NSURL(string:urlPath)!)
        request.setValue("application/json; charset=utf-8", forHTTPHeaderField:"Content-Type")
    
        request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.PrettyPrinted)
    
        request.HTTPMethod = "POST"
    
        print("Prepare to make request -> \(request)")
    
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
            if error != nil{
                print("Error -> \(error)")
                return
            }
    
            do {
                let result = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
    
                print("Result -> \(result)")
    
            } catch {
                print("Error -> \(error)")
            }
        }
    
        task.resume()
    

    It returns:

    Result -> Optional(["driver": <null>, "request_id": 5834384c-7283-4fe6-88a7-e74150c6ab30, "surge_multiplier": 1, "location": <null>, "vehicle": <null>, "status": processing, "eta": <null>])
    
    0 讨论(0)
提交回复
热议问题