1

我之前在这里看到过另一个类似的帖子,但没有太大帮助。无论如何,我已经完成了所有 OAuth 步骤,并获得了我的访问令牌。以下是我收到的错误 -

{"message":"不支持","code":"not_found"}

我尝试过手动使用 URl 并尝试过使用 POST。这是我尝试过的网址 -

http://sandbox-api.uber.com/v1/requests?access_token=m5MunZjxNVCXbg1p4DXPQjK76DYFaz&product_id=653e6788-871e-4c63-a018-d04423f5b2f7&start_latitude=40.11690903&start_longitude=-75.01428223&end_latitude=40.650729&end_longitude=-74.0095369

收到错误后,我尝试了一个 POST(感谢我在这里看到的另一篇文章)到沙箱请求,导致 301 Moved Permanently Response。

我使用的 POST 代码也在这里找到,我将在下面发布(我对这一切都很陌生)。

URL url4 = new URL("http://sandbox-api.uber.com/v1/requests");
Map<String,Object> params2 = new LinkedHashMap<>();
params2.put("access_token", AccessToken);
params2.put("product_id", productID);
params2.put("start_latitude", latitude);
params2.put("start_longitude", longitude) ;
params2.put("end_latitude", endLatitude);
params2.put("end_longitude", endLongitude);
StringBuilder postData = new StringBuilder();

for (Map.Entry<String,Object> param : params2.entrySet()) {
    if (postData.length() != 0) postData.append('&');
        postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
        postData.append('=');
                                      postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));        }                                       
            byte[] postDataBytes = postData.toString().getBytes("UTF-8");

HttpURLConnection conn = (HttpURLConnection)url4.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-formurlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));         
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);

Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for ( int c = in.read(); c != -1; c = in.read() )
    System.out.print((char)c);

那么我只是在这里做一些根本错误的事情吗?我已经得到了估计/产品的工作,所以我不确定我做错了什么。

为奇怪的格式道歉,这里是新的,它似乎不想让我缩进某些行。

谢谢

4

1 回答 1

1

您提供的代码存在一些问题:

  • 沙盒端点使用 HTTPS(而不是 HTTP)
  • 您应该通过 Authorization 标头提供访问令牌,而不是 URL 参数。
  • Content-Type 值应为 application/json。
于 2015-09-01T20:57:11.703 回答