Invalid OAuth2 Credentials in Uber SDK for android

2019-09-14 21:15发布

问题:

Below is my code to retrieve user profile. My login is successful , but when I try to fetch email address, I get the above error:

private void initializeUber(){
    if(!UberSdk.isInitialized()) {
        SessionConfiguration config = new SessionConfiguration.Builder()
                // mandatory
                .setClientId(AppConstants.UBER_CLIENT_ID)
                // required for enhanced button features
                .setServerToken(AppConstants.UBER_SERVER_TOKEN)
                // required scope for Ride Request Widget features
                .setScopes(Arrays.asList(Scope.PROFILE))
                // optional: set Sandbox as operating environment
                .setEnvironment(SessionConfiguration.Environment.SANDBOX)
                .build();

        UberSdk.initialize(config);
    }
}

private void checkLoginForUber() {

    LoginCallback loginCallback = new LoginCallback() {
        @Override
        public void onLoginCancel() {
            // User canceled login
            Log.i(TAG, "login cancelled");
        }

        @Override
        public void onLoginError(@NonNull AuthenticationError error) {
            // Error occurred during login
            Log.i(TAG, error.toString());
            Log.i(TAG, "login failed");
        }

        @Override
        public void onLoginSuccess(@NonNull AccessToken accessToken) {
            // Successful login!  The AccessToken will have already been saved.
            Log.i(TAG, "login successful");
            getUserProfile();
        }

        @Override
        public void onAuthorizationCodeReceived(@NonNull String authorizationCode) {
            Log.i(TAG, authorizationCode);
        }
    };
    accessTokenManager = new AccessTokenManager(this);
    loginManager = new LoginManager(accessTokenManager, loginCallback);
    loginManager.login(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    loginManager.onActivityResult(this, requestCode, resultCode, data);
}

private void getUserProfile(){
    try {
        AccessToken accessToken = accessTokenManager.getAccessToken();
        Log.i(TAG,accessToken.getToken());
        Session session = loginManager.getSession();
        AppContext.getmInstance().setSession(session);
        //Intent intent = new Intent(this, UbserService.class);
        //startService(intent);
        RidesService service = UberRidesApi.with(session).build().createService();
        service.getUserProfile().enqueue(new Callback<UserProfile>() {
            @Override
            public void onResponse(Call<UserProfile> call, Response<UserProfile> response) {
                Log.i(TAG, response.toString());
                if (response.isSuccessful()) {
                    //Success
                    UserProfile profile = response.body();
                    Log.i(TAG,profile.getEmail());
                } else {
                    //Api Failure
                    ApiError error = ErrorParser.parseError(response);
                    for (int i = 0; i < error.getClientErrors().size(); i++) {
                        Log.i(TAG, error.getClientErrors().get(i).getTitle());
                    }

                }
            }

            @Override
            public void onFailure(Call<UserProfile> call, Throwable t) {
                //Network Failure
            }
        });
    }catch (Exception ex){
        ex.printStackTrace();
    }
}

What I am doing here? The app redirects to Uber app, where I grant access to fetch user info. After I am back to my app, the login successful is called, but as I call getUserProfile, I get this error. I am using SSO for authentication.