CORS的优步API问题

时间:2017-03-19 22:45:14

标签: uber-api

第一次在这里问一个问题。我是这方面的初学者,但我真正难以接受我面临的问题。

正在使用的浏览器:

  • Safari和Firefox(均在Mac OS Sierra上)
  • Firefox(Linux - Ubuntu 16.04.2)

我已注册为优步开发者,并已在信息中心注册了应用。我目前只使用服务器令牌进行身份验证。在仪表板中,我在"授权"中设置了以下条目:用于CORS的应用程序选项卡(CORS支持的可选URI):

http://localhost:8000                  <-- web server in my PC
https://subdomain.mydomain.com         <--- remote web server

几个月前,我使用HTML,CSS和JS(使用Jquery v2.2.4)创建了一个Web应用程序来使用Ride Estimates API,并且能够成功地报告我所在区域中许多位置的数据。不知怎的,它不再起作用。我试图解决这个问题并改进功能。但是,由于之前不存在的CORS问题,我无法通过对API的初始查询。

我的API网址是:

https://api.uber.com/v1/estimates/price?start_latitude=8.969145&start_longitude=-79.5177675&end_latitude=8.984104&end_longitude=-79.517467&server_token={*********SERVER*TOKEN**********}

当我将其粘贴到浏览器的地址栏中时,我获得了有效的JSON:

{"prices":[{"localized_display_name":"uberX","distance":1.58,"display_name":"uberX","product_id":"811c3224-5554-4d29-98ae-c4366882011f","high_estimate":3,"surge_multiplier":1.0,"minimum":2,"low_estimate":2,"duration":420,"estimate":"2-3\u00a0$","currency_code":"USD"},{"localized_display_name":"X English","distance":1.58,"display_name":"X English","product_id":"8fe2c122-a4f0-43cc-97e0-ca5ef8b57fbc","high_estimate":4,"surge_multiplier":1.0,"minimum":3,"low_estimate":3,"duration":420,"estimate":"3-4\u00a0$","currency_code":"USD"},{"localized_display_name":"uberXL","distance":1.58,"display_name":"uberXL","product_id":"eb454d82-dcef-4d56-97ca-04cb11844ff2","high_estimate":4,"surge_multiplier":1.0,"minimum":3,"low_estimate":3,"duration":420,"estimate":"3-4\u00a0$","currency_code":"USD"},{"localized_display_name":"Uber Black","distance":1.58,"display_name":"Uber Black","product_id":"ba49000c-3b04-4f54-8d50-f7ae0e20e867","high_estimate":6,"surge_multiplier":1.0,"minimum":4,"low_estimate":4,"duration":420,"estimate":"4-6\u00a0$","currency_code":"USD"},{"localized_display_name":"Uber SUV","distance":1.58,"display_name":"Uber SUV","product_id":"65aaf0c2-655a-437d-bf72-5d935cf95ec9","high_estimate":7,"surge_multiplier":1.0,"minimum":5,"low_estimate":5,"duration":420,"estimate":"5-7\u00a0$","currency_code":"USD"}]}

然后我继续在网页中设置JS(w / JQuery)代码......

var url = "https://api.uber.com/v1/estimates/price?start_latitude=8.969145&start_longitude=-79.5177675&end_latitude=8.984104&end_longitude=-79.517467&server_token={*********SERVER*TOKEN**********}";

$.getJSON(url, function(result){
    console.log(result);
});

将HTML和JS上传到我的远程Web服务器,然后在我的任何浏览器中加载网页都会从Uber API中获得200状态。但是,控制台日志显示CORS阻止了我的请求(问题#1):

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.uber.com/v1/estimates/price?start_latitude=8.969145&start_longitude=-79.5177675&end_latitude=8.984104&end_longitude=-79.517467&server_token={*********SERVER*TOKEN**********}. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

然后,在两个Mac浏览器的检查器视图中,在网络/资源区域下,我看到来自GET请求的200状态消息。但是,与响应消息(问题#2)一起:

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

请求标题是:

GET /v1/estimates/price?start_latitude=8.969145&start_longitude=-79.5177675&end_latitude=8.984104&end_longitude=-79.517467&server_token={*********SERVER*TOKEN**********} HTTP/1.1
Host: api.uber.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://subdomain.domain.com/Uber/index.html
Origin: https://subdomain.domain.com
Connection: keep-alive

响应标题是:

HTTP/1.1 200 OK
Server: nginx
Date: Sun, 19 Mar 2017 22:26:31 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Content-Geo-System: wgs-84
Content-Language: en
X-Rate-Limit-Limit: 2000
X-Rate-Limit-Remaining: 1998
X-Rate-Limit-Reset: 1489964400
X-Uber-App: uberex-nonsandbox, optimus, migrator-uberex-optimus
Strict-Transport-Security: max-age=604800
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Encoding: gzip

在Firefox for Linux中,我有时候不会得到语法错误;我似乎总是在Mac浏览器上得到它。在Linux中,当我收到该错误时,然后单击&#34;编辑并重新发送&#34;标题按钮(重新发送标题但没有真正编辑标题),语法错误消失,响应文本实际显示应该在那里的Uber API对象...但我仍然在控制台日志上收到CORS阻止消息。我真的不明白为什么会这样,但这似乎是矛盾的。最后,我无法使用API​​数据,几个月前使用相同的方法,我可以获得几十个位置。

我已经在类似问题中寻找答案,但到目前为止还没有找到适合我案例的答案。任何帮助将不胜感激。真的很沮丧......真的被困在这里。

1 个答案:

答案 0 :(得分:1)

此问题是由API未正确包含标头引起的。此问题已得到解决,api现在正在按预期工作。此外,只有在请求中指定了原点时,才会在响应中返回allow origin标头。