Im integration uber ride request api. I successfully authenticated the uber account . I can get user history and user profile from uber api but I am not getting v1.2/requests/estimate.But when i request ride . using the below code ...
Im getting the response
{"message":"Invalid OAuth 2.0 credentials provided.","code":"unauthorized"}
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;
}
}
-------------------------------------------------------------------------------------------------------------------------1 Answer
It could be because there is no space provided between 'bearer' and token value in your header .
httpPost.setHeader("Authorization","Bearer"+token);
It needs to be
Authorization: Bearer <mytoken>
UPDATE
you are setting data values as name-value pairs. You have to set json if content type is json. Check this answer: How to send POST request in JSON using HTTPClient?
You have to set:
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);
----------------------------------------------------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment