Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google API Client OAuth Always Returns Canceled

I am trying to use the Google Fit History API and I am running into an issue where after I prompt the user for their Google account using ConnectionResult.StartResolutionForResult, I am ALWAYS getting a return code of CANCELED even though the user selects the account via the dialog. I have followed the guides found here (https://developers.google.com/fit/android/get-api-key) to the letter, as far as I can tell. I have a project in my Developers console. I have enabled the Fitness API in the console. And I have generated a client id using the debug keystore on my development machine. Here are some screenshots from developers console: Screenshot of API enabled in developer console Screenshot of OAuth Client ID in developer console

I am programming in Xamarin.Android and followed the example here. (Note that I do have Xamarin.GooglePlayServices.Fitness package installed): https://github.com/xamarin/monodroid-samples/tree/master/google-services/Fitness/BasicHistoryApi

Here are the key areas of the code:

    mClient = new GoogleApiClient.Builder (this)
        .AddApi (FitnessClass.HISTORY_API)
        .AddScope (new Scope (Scopes.FitnessActivityReadWrite))
        .AddConnectionCallbacks (clientConnectionCallback)
        .AddOnConnectionFailedListener (result => {
            Log.Info (TAG, "Connection failed. Cause: " + result);
            if (!result.HasResolution) {
                // Show the localized error dialog
                GooglePlayServicesUtil.GetErrorDialog (result.ErrorCode, this, 0).Show ();
                return;
            }
            // The failure has a resolution. Resolve it.
            // Called typically when the app is not yet authorized, and an
            // authorization dialog is displayed to the user.
            if (!authInProgress) {
                try {
                    Log.Info (TAG, "Attempting to resolve failed connection");
                    authInProgress = true;
                    result.StartResolutionForResult (this, REQUEST_OAUTH);
                } catch (IntentSender.SendIntentException e) {
                    Log.Error (TAG, "Exception while starting resolution activity", e);
                }
            }
        }).Build ();

...

    protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
    {
        if (requestCode == REQUEST_OAUTH) {
            authInProgress = false;
            if (resultCode == Result.Ok) {
                // Make sure the app is not already connected or attempting to connect
                if (!mClient.IsConnecting && !mClient.IsConnected) {
                    mClient.Connect ();
                }
            }
        }
    }

The OnFailedConnectionListener is getting called with statusCode=SIGN_IN_REQUIRED, which then causes me to call StartResolutionForResult and pop up the dialog for the user to select their Google Account. As soon as the dialog is displayed I am getting the following error in my LogCat. Note that this is before they select the account.

02-26 15:56:36.459: E/MDM(17800): [63567] b.run: Couldn't connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}

Once the user selects the account, OnActivityResult gets called and resultCode is always "Canceled", which is supposed to indicate the user dismissed the dialog but that is certainly not what happened here. Any help? It smells like something is wrong in Developer Console but after going through the guide 100 times with the same results I'm starting to go crazy.

like image 446
thedigitalsean Avatar asked Feb 26 '16 21:02

thedigitalsean


1 Answers

So my issue was that I was using the wrong debug.keystore. My Mac has both Android Studio and Xamarin Studio installed. I had incorrectly assumed that Xamarin was using "~/.android/debug.keystore" but it turns out that they put theirs in "~/.local/share/Xamarin/Mono for Android/debug.keystore" changing to using the SHA1 from this key fixed my issue. For my info on Xamarin keys: https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/MD5_SHA1/#OSX

like image 93
thedigitalsean Avatar answered Oct 20 '22 23:10

thedigitalsean