Google Oauth 2.0 RESULT_CANCELED while using Google Fit api

后端 未结 7 1983
死守一世寂寞
死守一世寂寞 2020-12-11 04:04

I am trying to use google fit api in my android app. I followed this guide and created SHA-1 certificate using keytool.exe in my jdk 1.8 bin folder. I have now created Oauth

7条回答
  •  甜味超标
    2020-12-11 04:24

    I ran into this problem recently as well. I'm not sure what the problem was but it may stem from Google's updates to the GoogleAPIClient library and authentication procedures.

    I had to update my own authentication flow to the latest documentation for Google Sign-Ins (Android) from this: https://developers.google.com/identity/sign-in/android/sign-in#before_you_begin

    I'm also using

        compile 'com.google.android.gms:play-services-auth:9.0.0'
        compile 'com.google.android.gms:play-services-fitness:9.0.0'
    

    for the libraries. Here's a snippet of my code:

    //runs an automated Google Fit connect sequence
    public static GoogleApiClient googleFitBuild(Activity activity, GoogleApiClient.ConnectionCallbacks connectionCallbacks, GoogleApiClient.OnConnectionFailedListener failedListener){
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                //.requestServerAuthCode(activity.getString(R.string.server_client_id), false)
                .requestEmail()
                .requestScopes(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE), new Scope(Scopes.FITNESS_BODY_READ_WRITE),
                        new Scope(Scopes.FITNESS_NUTRITION_READ_WRITE), new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
                .build();
        return new GoogleApiClient.Builder(activity)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .addConnectionCallbacks(connectionCallbacks)
                    .addOnConnectionFailedListener(failedListener)
                    //.addApi(Plus.API)
                    .addApi(Fitness.CONFIG_API)
                    .addApi(Fitness.HISTORY_API)
                    .addApi(Fitness.SESSIONS_API)
                    .addApi(Fitness.RECORDING_API)
                    .addApi(Fitness.BLE_API)
                    .addApi(Fitness.SENSORS_API)
                    .build();
    }
    
    //runs an automated Google Fit connect sequence
    public static void googleFitConnect(final Activity activity, final GoogleApiClient mGoogleApiClient){
        if(!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    Log.i(TAG, "Google API connected");
                    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
                    activity.startActivityForResult(signInIntent, FragmentLogin.REQUEST_OAUTH);
                }
                @Override
                public void onConnectionSuspended(int i) {
    
                }
            });
            mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
        }
    }
    

    GoogleApiClient googleApiClient = googleFitBuild(activity, (MainActivity)activity, (MainActivity)activity);
    googleFitConnect(activity, googleApiClient);
    

    I also had something close to the OP's code working before but it suddenly decided to stop working... frustrating to say the least.

提交回复
热议问题