IM集成Uber Ride请求API。我成功地验证了uber帐户。我可以从uber api获得用户历史和用户配置文件,但我没有得到v1.2/requests/estimate,而是在我请求ride时。使用下面的代码…
我得到了回应。
{“消息”:“提供的OAuth 2.0凭据无效。”,“代码”:“未授权”}
public JSONObject getFareid(String address,String product_id,floatstart_latitude,float start_longitude,float end_latitude,floatend_longitude,String token,String scope) {
try {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(address);
params.add(new BasicNameValuePair("product_id", product_id));
params.add(new BasicNameValuePair("start_latitude", "17.456f"));
params.add(new BasicNameValuePair("start_longitude", "78.3755f"));
params.add(new BasicNameValuePair("end_latitude", "17.3853f"));
params.add(new BasicNameValuePair("end_longitude","78.404f") );
params.add(new BasicNameValuePair("Authorization","Bearer"+token));
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept-Language", "en_US");
httpPost.setHeader("Authorization","Bearer"+token);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSONStr", json);
} catch (Exception e) {
e.getMessage();
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// Parse the String to a JSON Object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// Return JSON String
return jObj;
}
}
最佳答案
这可能是因为在头文件中的“承载者”和令牌值之间没有空格。httpPost.setHeader("Authorization","Bearer"+token);
必须Authorization: Bearer <mytoken>
更新
您正在将数据值设置为名称-值对。
如果内容类型是json,则必须设置json。
检查此答案:How to send POST request in JSON using HTTPClient?
你必须设置:
JSONObject holder = getJsonObjectFromMap(params);
//passes the results to a string builder/entity
StringEntity se = new StringEntity(holder.toString());
//sets the post request as the resulting string
httpost.setEntity(se);