Google Fit Authorization : AUTH_DENIED Authorization cancelled

2.3k Views Asked by At

I need immediate help about Authorization.

I cannot Authorize to Google Fit. I get AUTH_DENIED Authorization cancelled in my console. Here is what I did step by step. I am going to get crazy about it.

This is my function where I am trying to Authorize:

  const onCheckGoogleFit = () => {
    const options = {
      scopes: [
        Scopes.FITNESS_ACTIVITY_READ,
        Scopes.FITNESS_ACTIVITY_WRITE,
        Scopes.FITNESS_BODY_READ,
        Scopes.FITNESS_BODY_WRITE,
      ],
    };
    GoogleFit.authorize(options)
      .then((authResult) => {
        if (authResult.success) {
          console.log("AUTH_SUCCESS", authResult);
        } else {
          console.log("AUTH_DENIED", authResult.message);
        }
      })
      .catch(() => {
        console.log("AUTH_ERROR");
      });
  };

I did all the steps in this documentation:

To get SHA-1 Key, I ran this command in my RN project directory myProject/android/app :

keytool -list -v -keystore ./debug.keystore -alias androiddebugkey -storepass android -keypass android

OUTPUT:

Alias name: androiddebugkey
Creation date: May 18, 2021
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=...
Issuer: CN=...
Serial number: ...
Valid from: Tue May 18 11:29:52 TRT 2021 until: Sat Oct 03 11:29:52 TRT 2048
Certificate fingerprints:
         SHA1: [I got the key here.]
         SHA256: ...
Signature algorithm name: ...

Then in console.cloud.google.com:

  1. I enabled Google Fitness API
  2. I created new credentials for OAuth2 (selected User Data.)
  3. Skipped Scopes(optional)
  4. Application Type:Android
  5. Package Name founded from AndroidManifest.xml file. (com.example)
  6. Pasted SHA-1 Key which is taken from RNProject/android/app/debug.keystore(like in output above)
  7. Successfully got Client ID.

My RNProject/android/build.gradle I have this:

ext {
    buildToolsVersion = "29.0.2"
    minSdkVersion = 21
    compileSdkVersion = 29
    targetSdkVersion = 29
}

And I added this:

    maven {
        url 'https://maven.google.com'
    }

My RNProject/android/app/build.gradle

dependencies{
    // for google-fit
    implementation 'com.google.android.gms:play-services-fitness:20.0.0'
    implementation 'com.google.android.gms:play-services-auth:19.0.0'
    implementation "com.google.android.gms:play-services-base:+"
}
2

There are 2 best solutions below

1
On BEST ANSWER

So I found the solution. My steps were right. I just followed 2 steps to solve the problem.

  1. I changed my Package Name as (com.example.app) like in android/app/build.gradle in defaultConfig(applicationId). I used it as (com.example) at the first time like in AndroidManifest.xml file.

  2. I added my google account as a test user in console.cloud.google.com

And everything worked perfectly :)

0
On

In my scenario, I lost the keystore.jks key. I created a new keystore.jks and released a new build to play store. Thats where i experienced this issue. I had to redo the steps on console.cloud.google.com and pasted new SHA-1 key and got a clientID. Things where then working fine.