2016-12-03 83 views
3

Im整合超級通行證請求api。我成功驗證了超級帳戶。我可以從uber api獲取用戶歷史記錄和用戶資料,但我沒有獲得v1.2/requests/estimate。但是當我請求騎車時。使用下面的代碼...Uber無效的OAuth 2.0憑據提供優步認證在Android

我得到的迴應。

{ 「消息」: 「2.0憑證提供無效的OAuth。」, 「代碼」: 「未授權」}

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

爲什麼你正在做的所有的呼叫手動尤伯杯爲同一提供SDK在那裏你可以找到估計乘坐方法也會降低你的工作太 –

+0

我認爲'Authorization'只是一個頭部,而不是POST參數。然後再次,我沒有閱讀API文檔。除此之外,我建議你看看改進庫(或實際的優步Android SDK) –

+0

@ cricket_007優步sdk做他們正在使用改進 –

回答

1

這可能是因爲沒有 '承載' 之間設置空間和標題中的標記值。 httpPost.setHeader("Authorization","Bearer"+token);

它需要Authorization: Bearer <mytoken>

UPDATE

要設置數據值作爲名稱 - 值對。 如果內容類型是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); 
+0

然後它給出了這個錯誤{「message」:「無法解析JSON in請求正文。「,」code「:」invalid_json「} –

+0

更新回答 –

+0

感謝它的工作 –