Ravi Yadav
Ravi Yadav

Reputation: 11

BasicNetwork.performRequest: Unexpected response code 401

I am trying to get the access token from uber (i am able to receive the request_code). but when ever m trying to mke the call responce always show error

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

This is what the Document show how to get the 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')

this is how i am trying to get the 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;
                        }
                    };

Anybody have some solution... I think my code is not matching up with this

auth=(
    'INSERT_CLIENT_ID',
    'INSERT_CLIENT_SECRET',
)

Upvotes: 1

Views: 4454

Answers (2)

Alex Bitek
Alex Bitek

Reputation: 6557

According to the STEP THREE: GET AN ACCESS TOKEN from the Authentication docs the required parameters you need to send to the https://login.uber.com/oauth/v2/token to be able to exchange the code for an access_token are:

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

You are only sending the "code" and "grant_type" parameters, therefore you are unauthorized to access that Uber API endpoint.
The client_secret and client_id parameters are required to authenticate your request to the Uber server.

Also make sure you send the redirect_uri parameter which must match the one defined in the Uber Developer Dashboard for your app.

Upvotes: 0

Nas
Nas

Reputation: 2198

Below code may help you to get some idea. Change this things to your convenient code and the below one is for post request.

            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");

Upvotes: 1

Related Questions