Search code examples
androidgoogle-apigoogle-fitgoogle-fit-sdk

How should I set up OAuth consent screen without Android app verification?


I am working on an Android application which makes the use of Google Fit API. I have enabled it on Google Developer Console which provides the Client id and API key. How should I set up the OAuth screen?

My application uses the Sessions API for recording data and History API for viewing previously stored data. Currently, I am able to record the activity data which can be viewed in the Google Fit App but I am getting this error when I use the History API

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

I want to check the functionalities of the API just for demonstration, so no verification should be required for the app. How should I set up the consent screen or ask for user consent?
Do I require the verification for sensitive scopes like Activity Reading and Writing?


Solution

    1. Check you are requesting the correct scopes with associated Read/Write permissions: https://developers.google.com/fit/android/api-client-example
    val fitnessOptions = FitnessOptions.builder()
        .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
        .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
        .build()
    
    1. Check you are not attempting to read from scopes your don't have access to:

    i.e. change the data type DataType.AGGREGATE_STEP_COUNT_DELTA below in the response

    private fun accessGoogleFit() {
        val end = LocalDateTime.now()
        val start = end.minusYears(1)
        val endSeconds = end.atZone(ZoneId.systemDefault()).toEpochSecond()
        val startSeconds = start.atZone(ZoneId.systemDefault()).toEpochSecond()
    
        val readRequest = DataReadRequest.Builder()
            .aggregate(DataType.AGGREGATE_STEP_COUNT_DELTA)
            .setTimeRange(startSeconds, endSeconds, TimeUnit.SECONDS)
            .bucketByTime(1, TimeUnit.DAYS)
            .build()
        val account = GoogleSignIn.getAccountForExtension(this, fitnessOptions)
        Fitness.getHistoryClient(this, account)
            .readData(readRequest)
            .addOnSuccessListener({ response ->
                // Use response data here
                Log.i(TAG, "OnSuccess()")
            })
            .addOnFailureListener({ e -> Log.d(TAG, "OnFailure()", e) })
    }