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
One reason to get RESULT_CANCELED
is if your package name is diferent than the one you define in your app.
Double check that you are not setting another applicationId
in your build.gradle that could be different than package
defined in your manifest.
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.
In my case, I changed the apk name but failed to update it in the OAuth credentials console. Then my onActivityResult code gave me RESULT_CANCELLED. As soon as I corrected the apk name, everything started working again.
If the above solutions doesn't help you, then you should definitely consider this solution once. I literally struggled a lot to figure this out. So I think, my findings will help others to avoid same struggle and frustration.
I've followed the google guidelines to set up the GFit authentication. But upon finishing it, I noticed that when my app ask for user's permission, it pops up the authentication screen. But when the user selects one account, it just returns RESULT_CANCELED
in onActivityResult()
instead of RESULT_OK
. The reason behind that was certificate mismatch. I used my project's keystore file to generate the SHA-1
fingerprint which is required for OAuth token generation. But the build that I was deploying was a debug
one. When I tried with the release
build, it worked perfectly. The reason behind that was, Android Studio by default used a debug.keystore
file to sign the debug
builds which is different from the project's keystore file. That's why it didn't work in the debug mode. When I changed the debug
configuration for my project to use my project's keystore file, it worked like a charm. So, I recommend to check this usecase first. Most of the cases, this is the actual issue. You can find another StackOverflow question here which is addresses this issue.
To know more about how to change/modify the signing configurations for your builds, please refer to this.
I've encountered this problem just now. Took me a few hours to figure out, so i would like to point out for those who make the same mistake.
My Google API client cannot connect only on Production, everything works fine on Development environment.
This is because my app was set up to use "App Signing". With this, if you created the Oauth Client ID using the Production Keystore's SHA1 (or Upload Certificate's SHA1 in the picture), it will not be used, because google will remove this certificate and change to the "App Signing" certificate
So what we need to do to fix the problem is simply create a new OAuth Client ID with this new SHA1 like so:
The issue is quite easy to encounter because most of any tutorial will tell you to find your Production SHA1 and use it in the API console. But except for if you use "App signing" then it won't work any more.
I had this same issue and the error was that I was using building flavors (full, mock) which finally changes the package name. So, in the console, I had to put the package's flavor, not the manifest's one. Hope this helps :)