Does Uber API work with a local environment? Getti

2019-07-28 22:38发布

问题:

Im currently testing the Uber API, and I havent been able to get any results yet, since my webapp is getting a "Access-Control-Allow-Origin" issue:

XMLHttpRequest cannot load https://api.uber.com/v1/estimates/price. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost' is therefore not allowed access. The response had HTTP status code 422.

I've set already the origin URI: https://localhost

But the Access-Control-Allow-Origin error keeps coming. I will try to recreate the app on the Uber side, but Its kind of odd that it doesn't work the way its intended.

回答1:

You need to set http://localhost:{PORT NUMBER} as the allowed origin URI in the Authorizations tab of your developer dashboard. You'll want to update that URI to whichever you end up using in production.

See the documentation for CORS: https://developer.uber.com/docs/rides/api-reference



回答2:

Thanks for your msg 7imon7ays, it works also without the port.

It turns out that I got messed up with the LatLng Objects. The way I figured it out was, this: Since "https://api.uber.com/v1/estimates/price" is a GET I tested this url on a browser:

"https://api.uber.com/v1/estimates/price?start_latitude=37.625732&start_longitude=-122.377807&end_latitude=37.785114&end_longitude=-122.406677&server_token=xxxxxxx"

(replace xxxxxx with your server_token)...

And it worked!, I was able to see the output as expected, so digging deeper, I added a console.log(uberParams) <- to my uberParams, and it turns out that all of them but the key were undefined:

This is what I was using: (Wrong)

var uberParams = {
            start_latitude : origin.latitude,
            start_longitude : origin.longitude,
            end_latitude: destination.latitude,
            end_longitude: destination.longitude,
            server_token:"xxxxxxx"
        }

Where origin and destination are both google.maps.LatLng Objects. So, for the API, I was incorrectly sending this:

        {
            start_latitude : undefined,
            start_longitude : undefined,
            end_latitude: undefined,
            end_longitude: undefined,
            server_token:"xxxxxxx"
        }

And the UBER Api, was returning THIS ERROR: -> Access-Control-Allow-Origin

That caused the confusion, because the origin_uri wasn't the problem, as soon as I changed the params to this:

var uberParams = {
            start_latitude : origin.lat(),
            start_longitude : origin.lng(),
            end_latitude: destination.lat(),
            end_longitude: destination.lng(),
            server_token:"xxxxxxx"
        }

Everything worked as expected. A Note to my Uber Friends: "Great API, poor Error handling and descriptions..."



回答3:

On you dashboard, for example, if your redirection Url is http://localhost:8888/book/user/return

Then your origin Url should be http://localhost:8888 no slash at the end.



标签: uber-api