Google Fit throws application needs OAuth consent from the user exception

87 Views Asked by At

When trying to access user heart rate from my android application(kotlin) using google fit API I get the below error

com.google.android.gms.common.api.ApiException: 5000: Application needs OAuth consent from the user

I have added scope in AndroidManifest.xml

<uses-permission-sdk-23 android:name="android.permission.BODY_SENSORS" />

I check for permission in my onCreate() function in MainActivity.kt file

Below is my code for that.

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        FirebaseAuth.getInstance()
        val email=intent.getStringExtra("email")
        val name=intent.getStringExtra("name")
      
        if (ContextCompat.checkSelfPermission(this,BODY_SENSORS)
            != PackageManager.PERMISSION_GRANTED) {
            1+2;
            ActivityCompat.requestPermissions(this,
                arrayOf(BODY_SENSORS),
                MY_PERMISSIONS_REQUEST_ACTIVITY_RECOGNITION)
            // Permission is not granted
            }
}

Below is my code to retrieve heart rate data.

private fun accessGoogleFit() {
       
        val endTime = System.currentTimeMillis()
        val startTime = endTime - TimeUnit.HOURS.toMillis(1000)
  
        val readRequest = DataReadRequest.Builder()
            .read(DataType.TYPE_HEART_RATE_BPM)
            .enableServerQueries()
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build()

        val account = GoogleSignIn.getAccountForExtension(this, fitnessOptions)
        Fitness.getHistoryClient(this, account)
            .readData(readRequest)
            .addOnSuccessListener({ response ->
                Log.i(response.toString(), "OnSuccess()")
            })
            .addOnFailureListener({ e -> Log.d(TAG, "OnFailure()", e) })
    }

I have followed all the instructions given in this official documentation

Any idea on how to fix this? Do I have to add more configurations on google cloud console or in my kotlin application

0

There are 0 best solutions below