Java – Unable to connect to Google Play Services; Gets the cancellation status code

Unable to connect to Google Play Services; Gets the cancellation status code… here is a solution to the problem.

Unable to connect to Google Play Services; Gets the cancellation status code

So I’m trying to use Google Fit, but for some reason I can’t connect to Google Play services in my app. I’ve fully set up OAuth and tested it in another app and everything works. Look specifically at GoogleFitRepository, which is where I connect to the API.

I’m getting this error when trying to connect :

W/AutoManageHelper: Unresolved error while connecting client. Stopping auto-manage.
I/MyApp: Google Play services connection failed. Cause: ConnectionResult{statusCode=CANCELED, resolution=null, message=null}

GitHub link: https://github.com/drb56/FitnessExplorer

Any help would be appreciated. I’ve been stuck on this all day!

EDIT: Here is some code for the related class

public GoogleFitRepository(Activity activity)
{
    mClient = new GoogleApiClient.Builder(activity.getApplicationContext())
            .addApi(Fitness.HISTORY_API)
            .addApi(Fitness.SESSIONS_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
            .addConnectionCallbacks(this)
            .enableAutoManage((FragmentActivity)activity, 0, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.i("MyApp", "Google Play services connection failed. Cause: " +
                            result.toString());
                }})
            .build();

activityList = new ArrayList<>();
    activityDataPointList = new ArrayList<>();
    calorieActivitiesToday = new ArrayList<>();
    dayActivities = new ArrayList<>();
}

Solution

The new update requires the Google Signin API to be performed in conjunction with the Fit API.

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail()
        .requestScopes(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE), new Scope(Scopes.FITNESS_LOCATION_READ))
        .build();

googleApiClient = new GoogleApiClient.Builder(activity)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .addConnectionCallbacks(connectionCallbacks)
        .addOnConnectionFailedListener(failedListener)
        .addApi(Fitness.HISTORY_API)
        .addApi(Fitness.SESSIONS_API)
        .addApi(Fitness.RECORDING_API)
        .addApi(Fitness.SENSORS_API)
        .enableAutoManage(this, 0, this)
        .build();

You must enable the Auth API on console.developers.google.com. Please implement it server-side through the Google Auth API. Otherwise the above code will not run

Related Problems and Solutions