2016-12-03 75 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

感谢它的工作 –

相关问题