Search code examples
androidgoogle-apigoogle-oauthgoogle-fit

Google Sign in request to use Fitness API is always failing


I am following this tutorial to integrate Google Fit API

Steps that I did:

  1. Created a project in Firebase and configured the SHA-1 key in project settings.
  2. Downloaded Google Service JSON file and added it in project
  3. Enabled the Google Fit API in Google project console.
  4. Verified the auto-generated OAuth Client Ids are present in the credential section.

The Google sign-in request is always failing. Sharing the code below.

private fun connectToGoogleFit() {
    Log.e("GOOGLE_FIT", "google fit init")
    val fitnessOptions = FitnessOptions.builder()
        .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
        .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
        .build()
    if (!GoogleSignIn.hasPermissions(
            GoogleSignIn.getLastSignedInAccount(this),
            fitnessOptions
        )
    ) {
        Log.e("GOOGLE_FIT", "no permission")
        GoogleSignIn.requestPermissions(
            this, // your activity
            GOOGLE_FIT_PERMISSIONS_REQUEST_CODE,
            GoogleSignIn.getLastSignedInAccount(this),
            fitnessOptions
        )
    } else {
        Log.e("GOOGLE_FIT", "has permission")
        accessGoogleFit()
    }
}
private fun accessGoogleFit() {
    //todo
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == GOOGLE_FIT_PERMISSIONS_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            Log.e("GOOGLE_FIT", "connection success")
            accessGoogleFit()
        } else {
            Log.e("GOOGLE_FIT", "connection failed")
        }
    }
}

I can see an account selector dialog on running this code. I get the failure code in onActivityResult on selecting an account from the dialog.

The available docs seem to be very old. Did I miss anything?


Solution

  • i had the same problem.

    After more than a day i figure out the solution.

    In order to use Google Fit you must enable Google Provider in Authentication panel of your Firebase Project.

    Firebase Panel