BasicNetwork.performRequest:意外的响应代码401

时间:2016-03-08 09:20:56

标签: android android-volley uber-api

我正在尝试从uber获取访问令牌(我能够接收request_code)。但是,当我试图拨打电话时,响应始终显示错误

  

BasicNetwork.performRequest:https://login.uber.com/oauth/v2/token?code=6DAhQDdKQhE3ZD9aTekXRAW4SZDjcN&grant_type=authorization_code

的意外响应代码401

这是Document展示如何获取access_token

的内容
parameters = {
'redirect_uri': 'INSERT_ROUTE_TO_STEP_TWO',
'code': request.args.get('code'),
'grant_type': 'authorization_code',} 

response = requests.post(
'https://login.uber.com/oauth/token',
auth=(
    'INSERT_CLIENT_ID',
    'INSERT_CLIENT_SECRET',
),
data=parameters,)
access_token = response.json().get('access_token')

这就是我试图获取access_code的方式

String uber = new Uri.Builder()
                        .scheme("https")
                        .authority("login.uber.com")
                        .path("oauth/v2/token")
                        //.appendQueryParameter("server_token", "P9CQP52XZfA6zGPts8ZCGT2buhJoz80gTAroKUd")
                        .appendQueryParameter("code", code)
                        .appendQueryParameter("grant_type","authorization_code")
                        .build().toString();
                //final String requestBody = "grant_type=authorization_code";
                JsonObjectRequest jsonObjectRequest = jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, uber, null, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            try {
                                progressActivity.showContent();
                                SharedPref.saveUberToken(getApplicationContext(), jsonObject.getString("access_token"));
                                SharedPref.saveUberRefreshToken(getApplicationContext(),jsonObject.getString("refresh_token"));
                                OathUser();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {

                        }
                    }){
                        /**
                         * Passing some request headers *
                         * */
                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {
                            Map<String, String> headers = new HashMap<>();
                            headers.put("client_id", "IEXY6JG6y1AVqtlVZtzDOqcn3EYIBR6");
                            headers.put("client_secret", "7It74u9pb10ogZbvFro3EO3QNKm9Zscq3cogPVN");
                            return headers;
                        }
                    };

任何人都有一些解决方案...... 我认为我的代码与此

不匹配
auth=(
    'INSERT_CLIENT_ID',
    'INSERT_CLIENT_SECRET',
)

2 个答案:

答案 0 :(得分:1)

下面的代码可以帮助您了解一些信息。将此内容更改为方便的代码,下面的代码用于发布请求。

            URL url = new URL("https://xx.com/yy/");
            HttpsURLConnection urlConnection =
                    (HttpsURLConnection) url.openConnection();


            String publicKey = "test_key:pass";
            String encodedString = Base64.encodeToString(publicKey.getBytes(), Base64.NO_WRAP);
            String basicAuth = "Basic ".concat(encodedString));


            //setting header for authentication purpose, should use authorization as keyword if you change this it won't work anymore
            urlConnection.setRequestProperty("Authorization", basicAuth);

            //setting other url options
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setConnectTimeout(15000);
            urlConnection.setReadTimeout(15000);
            urlConnection.setRequestMethod("POST");

答案 1 :(得分:0)

根据STEP THREE: GET AN ACCESS TOKENAuthentication docs所需的参数,您需要发送到https://login.uber.com/oauth/v2/token以便能够为access_token交换代码:

client_secret=YOUR_CLIENT_SECRET
client_id=YOUR_CLIENT_ID
grant_type=authorization_code
redirect_uri=YOUR_REDIRECT_URI
code=AUTHORIZATION_CODE_FROM_STEP_2

您只发送“code”和“grant_type”参数,因此您无权访问该Uber API端点。
需要client_secret和client_id参数来验证您对Uber服务器的请求。

另外,请务必发送redirect_uri参数,该参数必须与您应用的Uber Developer Dashboard中定义的参数匹配。