Search code examples
androidgoogle-authenticationgoogle-fit

Google Auth signin RESULT_CANCELED after refactoring package name


I have refactored my app package name.I need to signin to my google fit account. But this is unsuccessful even after following the steps below.

  1. I have refactored the package name of my app.
  2. I have changed the application id in the gradle file
  3. I have downloaded the updated google-services json
  4. I have changed the name in the config file.

I am at my wits end trying to find the solution to this problem. The status code of the error is 4.

public GoogleApiClient googleFitAuthBuild(Activity activity, GoogleApiClient.ConnectionCallbacks connectionCallbacks, GoogleApiClient.OnConnectionFailedListener failedListener) {
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestId()
            .requestProfile()
            .requestScopes(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE), new Scope(Scopes.FITNESS_BODY_READ_WRITE), new Scope(Scopes.PROFILE), new Scope(Scopes.FITNESS_NUTRITION_READ_WRITE))
            .build();

    return new GoogleApiClient.Builder(activity)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .addConnectionCallbacks(connectionCallbacks)
            .addOnConnectionFailedListener(failedListener)
            .addApi(Fitness.HISTORY_API)
            .addApi(Fitness.SESSIONS_API)
            .addApi(Fitness.RECORDING_API)
            .addApi(Fitness.SENSORS_API)
            .enableAutoManage(this, 0, failedListener)
            .build();
}


  public void googleApiConnect(final Activity activity, final GoogleApiClient mGoogleApiClient) {
    mGoogleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
        @Override
        public void onConnected(Bundle bundle) {
            S.L("Google API connected");
            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            activity.startActivityForResult(signInIntent, S.REQUEST_OAUTH);
        }

        @Override
        public void onConnectionSuspended(int i) {
            S.L("FITCONNECT suspended i=" + i);
        }
    });
    mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == S.REQUEST_OAUTH) {
        if (resultCode == RESULT_OK) {

            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            GoogleSignInAccount account = result.getSignInAccount();

        } else if (resultCode == RESULT_CANCELED) {
            S.L("RESULT_CANCELED");
        }
    } else {
        S.L("requestCode NOT S.REQUEST_OAUTH");
    }
}

The output is RESULT_CANCELED. this code was working before I refactored the app package name.


Solution

  • The issue was that the refactored package name belongs to my published app. (I want to publish a new version of the app). But when I tested the debug version, it could not find the key store associated with the published app with the package name. I suspected this and I published the new version. This published app does not have the issue mentioned as it is signed with the same key store.