I have Google Sign In Integrated in my application as an option. Now I'm integrating Google Fit to read data from it. But the issue is during permission request for Fitness data it doesn't provide an option to choose from Google accounts present on Device. Here is the code I'm using for requesting permission:
FitnessOptions fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.build();
GoogleSignIn.requestPermissions(
MyActivity.this, // your activity
GOOGLE_FIT_PERMISSIONS_REQUEST_CODE,
null, // passing null specifically to ask for account selection
fitnessOptions);
This code moves direct to permission page with the account used for Google Sign In. When I use other sign in option e.g. Facebook where Google SignIn is not called, the above code shows the account selection dialog perfectly.
This creates problem when user needs different email accounts for Google Sign In and Fitbit.
Is there any solution to have separate accounts for Google fit and Google Sign In? P.S: my google sign in email is linked with Firebase and backend logic, so asking user to use Google account associated with Google Fit will not help in my case
Although I didn't find the solution to using two google accounts in my app, I solved my use case using following setup:
Since I'm using Google Login with Firebase and in my app Firebase is the source for the user authentication, I simply calls logout on Goolge Auth after authentication with firebase to remove Google auth and hence allowing selection of google account for Google Fit connect.
AuthCredential credential =
GoogleAuthProvider.getCredential(googleSignInAccount.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (App.getGoogleApiHelper().isConnected()) {
// after checking if google client is available to signout from google auth
Auth.GoogleSignInApi.signOut(App.getGoogleApiHelper().getGoogleApiClient());
}
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Timber.tag(TAG).d("signInWithCredential: success");
} else {
// If sign in fails, display a message to the user.
Timber.tag(TAG).d(task.getException(), "signInWithCredential:failure");
}
}
});