Skip to content Skip to sidebar Skip to footer

How To Get Uber Access Token Using Authorization Code In Login Callback

I am configuring uber sdk with Request scope (Restricted). In the LoginManager callback method onAuthorizationCodeReceived() I am getting authorizationCode as a parameter, whereas

Solution 1:

You posted SO question as well. Plase, use the authorization code as per this sample you submitted - because it is confirmed from our logs that you managed to get a valid access token as a response (KA.eyJ2ZXJzaW9uIjoyLCJ*****). Also, you successfully initiate new ride request. So code you used to achieve it is a valid code. If this does not answer your question please check Android SDK sample.

Solution 2:

Yes, you are right that I manage to get the access token using the authorization code. I have gone through your REST web service doc and found out that using a POST request to server with a valid authorization code, I can get the required parameters to generate an access token.

Here is my updated code for getting auth code in the callback and request server using HTTP POST method to generate access token...

loginManager = newLoginManager(accessTokenManager,
                newLoginCallback() {

                    @OverridepublicvoidonLoginCancel() {
                        Toast.makeText(CustomActivity2.this, "Login cancelled", Toast.LENGTH_LONG).show();
                    }

                    @OverridepublicvoidonLoginError(@NonNull AuthenticationError error) {
                        Toast.makeText(CustomActivity2.this,
                                "Error: "+error.name(), Toast.LENGTH_LONG)
                                .show();
                    }

                    @OverridepublicvoidonLoginSuccess(@NonNull AccessToken accessToken) {
                        Toast.makeText(CustomActivity2.this, "Login success",
                                Toast.LENGTH_LONG)
                                .show();
                    }

                    @OverridepublicvoidonAuthorizationCodeReceived(@NonNull String authorizationCode) {
                        Toast.makeText(CustomActivity2.this, "Your Auth code is: "+authorizationCode,
                                Toast.LENGTH_LONG)
                                .show();
                        Ion.with(getApplicationContext())
                                .load("POST", "https://login.uber.com/oauth/v2/token")
                                .setBodyParameter("client_id",getResources().getString(R.string.client_id))
                                .setBodyParameter("client_secret",getResources().getString(R.string.client_secret))
                                .setBodyParameter("grant_type","authorization_code")
                                .setBodyParameter("redirect_uri",getResources().getString(R.string.redirect_url))
                                .setBodyParameter("code",authorizationCode)
                                .setBodyParameter("scope","profile history request")
                                .asString()
                                .setCallback(newFutureCallback<String>() {
                                    @OverridepublicvoidonCompleted(Exception e, String result) {
                                        try {
                                            JSONObjectjsonObject=newJSONObject(result);
                                            if (result.contains("error")) {
                                                Stringerror= jsonObject.getString("error");
                                                Toast.makeText(CustomActivity2.this, error, Toast.LENGTH_SHORT).show();
                                            }else {
                                                StringaccessTokenTemp= jsonObject.getString("access_token");
                                                StringexpiresInTemp= jsonObject.getString("expires_in");
                                                StringlastAuthenticatedTemp= jsonObject.getString("last_authenticated");
                                                StringrefreshTokenTemp= jsonObject.getString("refresh_token");
                                                StringscopeTemp= jsonObject.getString("scope");
                                                StringtokenTypeTemp= jsonObject.getString("token_type");

                                                AccessTokenManageraccessTokenManager=newAccessTokenManager(getApplicationContext());
                                                intexpirationTime= Integer.parseInt(expiresInTemp);
                                                List<Scope> scopes = Arrays.asList(Scope.PROFILE, Scope.HISTORY, Scope.REQUEST);
                                                Stringtoken= accessTokenTemp;
                                                StringrefreshToken= refreshTokenTemp;
                                                StringtokenType= tokenTypeTemp;
                                                AccessTokenaccessToken=newAccessToken(expirationTime, scopes, token, refreshToken, tokenType);
                                                accessTokenManager.setAccessToken(accessToken);

                                                if (accessTokenManager.getAccessToken() != null){
                                                    createSession();
                                                }
                                            }
                                        } catch (JSONException e1) {
                                            e1.printStackTrace();
                                        }
                                    }
                                });
                    }
                },
                config,
                1113).setRedirectForAuthorizationCode(true);

The problem with the API is there is no mention of requesting server to POST method for getting the access token. Also there is no mention of the onAuthorizationCodeReceived() callback method in the doc.

And also, there should be a JAVA interface for doing this operation as everything else is doing that (OR at least in the doc they should mention the process).

Whatever, I have found out the solution yesterday evening on my own (14 hours ego) and now (today morning) I am posting the solution. Hope, this will help someone.

NOTE: I have used ION library as a network client.

Post a Comment for "How To Get Uber Access Token Using Authorization Code In Login Callback"