How to disable Google Fit and revoke permissions from the app itself

蓝咒 提交于 2020-01-14 07:42:07

问题


I've setup app that connects to Google Fit, reads and writes some data about users body. When user disables Google Fit in apps settings, I try to revoke my apps permissions for by calling:

public void disableGoogleFit(){
    if(!mClient.isConnected()){
        Log.e(TAG, "Google Fit wasn't connected");
        return;
    }
    PendingResult<Status> pendingResult = Fitness.ConfigApi.disableFit(mClient);

    pendingResult.setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(Status status) {
            if(status.isSuccess()) {
                Log.i(TAG, "Google Fit disabled");
            }else{
                Log.e(TAG, "Google Fit wasn't disabled " + status);
            }
        }
    });
}

Even though I could successfully read/write data, disabling Fit returns me error:

Google Fit wasn't disabled Status
{statusCode=unknown status code: 5010, resolution=null}

Edit1: Added whole method, in which its visible, that client is connected at the moment I try do disable Fit.


回答1:


I ran into this issue if I revoked access on the Google Fit side of the operation as opposed to calling the disableFit() method. When using the disableFit() method, things got disconnected just fine and re-connecting was a cake walk. But When Google Fit revoked access it does not eliminate the existing OAuth and so you are stuck in limbo. Limbo being you are disconnected but no OAuth challenge is issued so you keep getting the 5010 error.

The only solution I came up with was to OAuth challenge by connecting to another account. Then you were fine. This sounds like an issue on Google Fit, however, and nothing on the client side.




回答2:


Currently with 15.0.1 lib it can be easily done:

fun disconnect(context: Context) {
    val fitnessOptions = FitnessOptions.builder()
            .addDataType(DataType.TYPE_WORKOUT_EXERCISE, FitnessOptions.ACCESS_READ)
            .build()
    val signInOptions = GoogleSignInOptions.Builder().addExtension(fitnessOptions).build()
    val client = GoogleSignIn.getClient(context, signInOptions)
    client.revokeAccess()
}



回答3:


I also faced similar problem.

This issue occurs if app is not registered properly in google developer console.

I think you have registered app on Google developer Console using production keystore certificate fingerprint(SHA1) where as you are testing it on app which has debug keystore.

Perform following steps:

  1. Create one more client Id using debug keystore certificate fingerprint(SHA1).
  2. Uninstall existing app.
  3. Install app & connect to Google fit.
  4. Check under google fit app & make sure that your app is listed as a connected app (... > Settings > Connected apps > Connected apps & devices).
  5. Now run the above code & it will work !!!



回答4:


As per the Android developer forum

https://developer.android.com/reference/com/google/android/gms/fitness/ConfigApi.html

public static final int APP_NOT_FIT_ENABLED

Status code denotes that an app was not found in the list of connected apps in Google Fit. Signifies that either access to the app was already revoked, or the app is not registered on the developer's console.

Constant Value: 5010 (0x00001392)

and to call a disableFit, client must be connected at the time of the call

public abstract PendingResult<Status> disableFit (GoogleApiClient client)

Disables Google Fit for an app. Can be used to revoke all granted OAuth access permissions from an app and consequently remove all existing subscriptions and registrations of the app.

Parameters

client an existing GoogleApiClient. Must be connected at the time of this call.




回答5:


After disabling call

mClient.disconnect();

Which is worked for me



来源:https://stackoverflow.com/questions/27130853/how-to-disable-google-fit-and-revoke-permissions-from-the-app-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!