Uber 在 android 中提供的 Uber 身份验证无效 OAuth 2.0 凭据
Uber Invalid OAuth 2.0 credentials provided Uber Authentication In android
我正在整合优步乘车请求 api。我成功验证了 uber 帐户。我可以从 uber api 获取用户历史记录和用户配置文件,但我没有获取 v1.2/requests/estimate。但是当我请求乘车时。使用下面的代码...
我收到回复了。
{"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;
}
}
可能是因为 'bearer' 和您的 header 中的令牌值之间没有提供 space。 httpPost.setHeader("Authorization","Bearer"+token);
需要Authorization: Bearer <mytoken>
更新
您正在将数据值设置为 name-value 对。
如果内容类型为 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);
我正在整合优步乘车请求 api。我成功验证了 uber 帐户。我可以从 uber api 获取用户历史记录和用户配置文件,但我没有获取 v1.2/requests/estimate。但是当我请求乘车时。使用下面的代码...
我收到回复了。
{"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;
}
}
可能是因为 'bearer' 和您的 header 中的令牌值之间没有提供 space。 httpPost.setHeader("Authorization","Bearer"+token);
需要Authorization: Bearer <mytoken>
更新
您正在将数据值设置为 name-value 对。 如果内容类型为 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);