1. Are you ready for the Galaxy S20? Here is everything we know so far!

runtime error while accessing google api in android emulator

Discussion in 'Android Development' started by raksheg, Jan 28, 2021.

  1. raksheg

    raksheg Lurker
    Thread Starter

    I am trying to access the googleFit API. It seems pretty straightforward. Get the google sign-in permissions and required authorizations then query for Step count. My code doesn't seem to work. and it logs only "Error!!" that's it. Android gurus, Where am I going wrong??

    Code (Text):
    1. fun getAuthorizationAndReadData() {
    2.         try {
    3.             MainActivity().fitSignIn(FitActionRequestCode.READ_DATA)
    4.         } catch () {
    5.            Log.i("e", "error!!!!")
    6.         }
    7.        }
    MainActivity.kt
    Code (Text):
    1. enum class FitActionRequestCode {
    2.     READ_DATA
    3. }
    4.  
    5. private val fitnessOptions: GoogleSignInOptionsExtension = FitnessOptions.builder()
    6.             .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ).build()
    7.  
    8.     fun fitSignIn(requestCode: FitActionRequestCode) {
    9.         if (oAuthPermissionsApproved()) {
    10.             readHistoryData()
    11.         } else {
    12.             requestCode.let {
    13.                 GoogleSignIn.requestPermissions(
    14.                         this,
    15.                         requestCode.ordinal,
    16.                         getGoogleAccount(), fitnessOptions)
    17.             }
    18.         }
    19.     }
    20.  
    21.     private fun getGoogleAccount() = GoogleSignIn.getAccountForExtension(this, fitnessOptions)
    22.  
    23.  
    24.     private fun oAuthPermissionsApproved() = GoogleSignIn.hasPermissions(getGoogleAccount(), fitnessOptions)
    25.  
    26.     private fun performActionForRequestCode(requestCode: FitActionRequestCode) = when (requestCode) {
    27.         FitActionRequestCode.READ_DATA -> readHistoryData()
    28.     }
    29.     private fun readHistoryData(): Task<DataReadResponse> {
    30.         // Begin by creating the query.
    31.         val readRequest = queryFitnessData()
    32.  
    33.         // Invoke the History API to fetch the data with the query
    34.         return Fitness.getHistoryClient(this, getGoogleAccount())
    35.                 .readData(readRequest)
    36.                 .addOnSuccessListener { dataReadResponse ->
    37.                     printData(dataReadResponse)
    38.                     Log.i(ContentValues.TAG, "Data read was successful!") }
    39.                 .addOnFailureListener { e ->
    40.                     Log.e(ContentValues.TAG, "There was a problem reading the data.", e)
    41.                 }
    42.     }
    43.  
    44.     private fun queryFitnessData(): DataReadRequest {
    45.         // [START build_read_data_request]
    46.         // Setting a start and end date using a range of 1 week before this moment.
    47.         val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
    48.         val now = Date()
    49.         calendar.time = now
    50.         val endTime = calendar.timeInMillis
    51.         calendar.add(Calendar.WEEK_OF_YEAR, -1)
    52.         val startTime = calendar.timeInMillis
    53.  
    54.         return DataReadRequest.Builder()
    55.                 .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
    56.                 .bucketByTime(1, TimeUnit.DAYS)
    57.                 .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
    58.                 .build()
    59.     }
    I have an app that needs to access Google Fit Api.
    I can get the google sign-in pop up. But the next screen, requesting permission for my data types (TYPE_STEP_COUNT_DELTA, AGGREGATE_STEP_COUNT_DELTA) is not displayed.

    As soon as I choose my google account the pop-up goes away and nothing is displayed further.

    [​IMG]

    I have followed all the steps for the Setup mentioned here https://developers.google.com/fit/android/get-started

    1. Set up my project in Google API developer console

    2. Install google play services packages in my android studio

    3. Created an OAuth 2.0 Client ID [​IMG]

    4. Add the dependencies
    Code (Text):
    1.   dependencies {
    2.        implementation 'com.google.android.gms:play-services-fitness:20.0.0'
    3.        implementation 'com.google.android.gms:play-services-auth:19.0.0'
    4.    }
    1. Enable sign-in options "google" in my firebase console
    Yet I do not get the further screens.

    My code is as below:


    Code (Text):
    1. public void setFitnessOption() {
    2.        fitnessOptions =
    3.                FitnessOptions.builder()
    4.                        .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
    5.                        .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
    6.                        .build();
    7.      }
    8.  
    9. public void checkFitInstalled() {
    10.        if (isGoogleFitPermissionGranted()) {
    11.            GetData();  //step count query    
    12.        } else {
    13.          requestGoogleFitPermission();
    14.            GetData();    //step count query
    15.        }
    16.      }
    17.  
    18. public boolean isGoogleFitPermissionGranted() {
    19.        if (GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions)) {
    20.          return true;
    21.        } else {
    22.          return false;
    23.        }
    24.      }
    25.  
    26. public void requestGoogleFitPermission() {
    27.        GoogleSignInAccount account = GoogleSignIn.getAccountForExtension(this, fitnessOptions);
    28.        GoogleSignIn.requestPermissions(
    29.                this,
    30.                GOOGLE_FIT_PERMISSIONS_REQUEST_CODE,
    31.                account,
    32.                fitnessOptions);
    33.      }

    What am I missing out?? struggling for days with this issue. Any input would be great!!!
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - runtime error while
  1. hhamzalashari
    Replies:
    3
    Views:
    304
  2. marietto2008
    Replies:
    0
    Views:
    534
  3. sejwalsakshi
    Replies:
    3
    Views:
    693
  4. ciabatron
    Replies:
    2
    Views:
    351
  5. Saima045
    Replies:
    1
    Views:
    370
  6. MoodyBlues
    Replies:
    6
    Views:
    557
  7. swaroopandroid
    Replies:
    1
    Views:
    453
  8. ashish VIVId
    Replies:
    0
    Views:
    426
  9. EverydayDiesel
    Replies:
    1
    Views:
    620
  10. Dawit Abraham
    Replies:
    0
    Views:
    319

Share This Page

Loading...