3

So I'm trying to work with Google Fit and for some reason I can't connect to Google Play Services in my app. I have the OAuth all set up completely and I tested it in another app and it all worked just fine. Specifically look at the GoogleFitRepository, this is where I'm connecting 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}

Link to GitHub: https://github.com/drb56/FitnessExplorer

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

Edit: here's some code from the class in question

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<>();
}
suku
  • 10,507
  • 16
  • 75
  • 120
user292277
  • 101
  • 2
  • 5

2 Answers2

6

The new update requires google signin api to be executed along 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 have to enable Auth API on console.developers.google.com. Please go through the server side implementation of Google Auth API. Otherwise the above code will not work

suku
  • 10,507
  • 16
  • 75
  • 120
  • But I have another application that doesn't require the GoogleSignInOptions. When did this become a requirement? – user292277 Jun 27 '16 at 16:22
  • recently...about a month ago...none of the tutorials are updated – suku Jun 28 '16 at 02:22
  • So it's still not working, even when I add that. In fact my tester program isn't even working when I use that. It gives me no data. Then when I comment it out it gives me the data I need. – user292277 Jun 28 '16 at 13:23
  • Here's a solution given for the canceled status: https://github.com/googlesamples/android-fit/issues/15 – suku Jun 28 '16 at 16:07
  • @suku - the suggested sign in code does not work. It returns an error that says sign in is required. Any suggestions? – MadProgrammer Feb 13 '17 at 17:49
  • @MadProgrammer, it is because you have not authorized google login. You have enable google login in console.developers.google.com. Because the auth process is returning an error, Fit api cannot get registered hence it asking you to sign-in – suku Feb 14 '17 at 00:16
  • @suku did you mean Oauth? Because I have this one turned on and it still throws that error.. – Phantomazi Feb 15 '17 at 13:20
  • Yes...just confirm if you are able to do a google login. If you are then, probably log out and retry logging in with the new scopes – suku Feb 15 '17 at 13:52
  • 1
    Alright, fixed my issues, but it was tricky for one sole reason - I never realised my Oauth key was for my debug.keystore, not for the release. So for the next reader - make sure that you have generated your Oauth for the release variant, not for the debug one. – Phantomazi Feb 15 '17 at 14:45
0

I had same problem, when working with GoogleFit from library. After long research I found, that I have to put:

   <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

into the manifest of application and not into the manifest of library.

Julia
  • 1
  • 2