title: How to get Google account data to fetch steps from Google Fit API on Android?
body: <p>I want to access number of steps from Google Fit API on Android. I have a following piece of code and I execute <code>tryToAccessStepsFromGoogleFit()</code> when a button is clicked:</p>
  <pre><code>val FITNESS_OPTIONS: FitnessOptions = FitnessOptions.builder()
          .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
          .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
          .build()
  
  private fun tryToAccessStepsFromGoogleFit() {
      val account = GoogleSignIn.getAccountForExtension(requireContext(), FITNESS_OPTIONS)
      // all fields of account are empty
  
      if (!GoogleSignIn.hasPermissions(account, FITNESS_OPTIONS)) {
          Log.i(TAG, &quot;Asking for permission&quot;)
          GoogleSignIn.requestPermissions(
              this,
              GOOGLE_FIT_STEPS_PERMISSIONS_REQUEST_CODE,
              account,
              FITNESS_OPTIONS
          )
      } else { // this branch is executed which is weird
          Log.i(TAG, &quot;Already has permissions&quot;)
          accessStepsFromGoogleFit()
      }
  }
  
  private fun accessStepsFromGoogleFit() {
      Log.i(TAG, &quot;Accessing steps from Google Fit&quot;)
  
      val account = GoogleSignIn.getAccountForExtension(requireContext(), FITNESS_OPTIONS)
  
      Fitness.getHistoryClient(this.activity!!, account)
          .readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
          .addOnSuccessListener { result -&gt;
              // do whatever with the result
          }
          .addOnFailureListener { e -&gt;
              Log.i(TAG, &quot;There was a problem getting steps: &quot; + e.localizedMessage)
              Toast.makeText(context, R.string.permission_not_granted, Toast.LENGTH_LONG).show()
              // this branch is executed and error happens &quot;4: The user must be signed in to make this API call.&quot;
          }
  }
  </code></pre>
  <p>which is more or less similar to the one in Google Fit android API docs.</p>
  <p>The problem is that <code>getAccountForExtension</code> doesn't seem to return any sensible account data nor showing any intent to prompt user to choose their account and grant access to Google Fit data. <strong>What should I do to achieve that?</strong></p>
  <hr />
  <p>Also, on SO I've found code that shows a dialog to choose an account:</p>
  <pre><code>private fun startSignInIntent() {
      val signInClient: GoogleSignInClient = GoogleSignIn.getClient(
          this.activity!!,
          GoogleSignInOptions.DEFAULT_SIGN_IN
      )
      val intent = signInClient.signInIntent
      startActivityForResult(intent, GOOGLE_ACCOUNT_CHOSEN)
  }
  </code></pre>
  <p>Showing the above dialog and then using <code>GoogleSignIn.getAccountForExtension</code> allows to fetch the data from the Fitness API but the documentation says nothing about that.</p>
  <hr />
  <p>Possibly helpful info:</p>
  <ul>
  <li>SDK target version: 29</li>
  <li>Android 9</li>
  <li>tested on MIUI Global 11.0.4</li>
  <li>I've already created a project in Google Developers Console, provided SHA1, enabled Fitness for this project, etc.</li>
  </ul>