【问题标题】:Uber Invalid OAuth 2.0 credentials provided Uber Authentication In androidUber 在 android 中提供了无效的 OAuth 2.0 凭据 Uber 身份验证
【发布时间】:2017-04-18 03:33:32
【问题描述】:

我集成了 uber 乘车请求 api。我成功验证了 uber 帐户。我可以从 uber api 获取用户历史记录和用户个人资料,但我没有得到 v1.2/requests/estimate。但是当我请求搭车时。使用下面的代码...

我得到了回应。

{"message":"提供的 OAuth 2.0 凭据无效。","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;
}

}

【问题讨论】:

  • 为什么您要手动进行所有呼叫 uber 提供了同样的 sdk,您可以在其中找到估算骑行的方法,它也会减少您的工作
  • 我认为 Authorization 只是一个标题,而不是一个 POST 参数。再说一次,我没有阅读 API 文档。除此之外,我建议您查看 Retrofit 库(或实际的 Uber Android SDK)
  • @cricket_007 uber sdk 正在做同样的事情,他们正在使用改造

标签: android uber-api


【解决方案1】:

这可能是因为您的 header 中的 'bearer' 和 token 值之间没有提供空格。 httpPost.setHeader("Authorization","Bearer"+token);

必须是Authorization: Bearer <mytoken>

更新

您正在将数据值设置为名称-值对。 如果内容类型是 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);

【讨论】:

  • 然后它给出了这个错误 {"message":"Unable to parse JSON in request body.","code":"invalid_json"}
  • 更新答案
  • 没问题 :) 不过我同意@cricket_007。您应该使用 sdk
猜你喜欢
  • 2016-08-28
  • 2015-07-05
  • 1970-01-01
  • 2016-08-02
  • 2017-02-18
  • 1970-01-01
  • 2017-05-04
  • 2020-02-16
  • 1970-01-01
相关资源
最近更新 更多