4

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;
}

}
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
Rohan Chavan
  • 177
  • 1
  • 9
  • 1
    why you are doing all call manually uber is providing sdk for same where you can find method for estimate ride it will also reduce your work too – Manthan Patel Dec 03 '16 at 09:51
  • I think `Authorization` is a header only, not a POST parameter. Then again, I'm not reading the API docs. Besides that, I'd suggest you look into the Retrofit library (or the actual Uber Android SDK) – OneCricketeer Dec 03 '16 at 09:53
  • @cricket_007 uber sdk is doing same they are using retrofit – Manthan Patel Dec 03 '16 at 09:55

1 Answers1

1

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);
Community
  • 1
  • 1
Suraj Rao
  • 28,186
  • 10
  • 88
  • 94