Google API 客户端 OAuth 始终 Returns 已取消

Google API Client OAuth Always Returns Canceled

我正在尝试使用 Google 拟合历史记录 API,但我 运行 遇到了一个问题,在我提示用户输入他们的 Google 帐户后,我使用 ConnectionResult.StartResolutionForResult,我总是收到 return 已取消的代码,即使用户 select 通过对话框设置了帐户。据我所知,我已严格按照此处 (https://developers.google.com/fit/android/get-api-key) 中的指南进行操作。我的开发人员控制台中有一个项目。我在控制台中启用了 Fitness API。我在我的开发机器上使用调试密钥库生成了一个客户端 ID。以下是开发人员控制台的一些屏幕截图:

我正在 Xamarin.Android 中编程,并按照此处的示例进行操作。 (请注意,我确实安装了 Xamarin.GooglePlayServices.Fitness 包): https://github.com/xamarin/monodroid-samples/tree/master/google-services/Fitness/BasicHistoryApi

以下是代码的关键区域:

    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 ();
                }
            }
        }
    }

OnFailedConnectionListener 正在使用 statusCode=SIGN_IN_REQUIRED 进行调用,这导致我调用 StartResolutionForResult 并弹出对话框供用户 select 他们的 Google 帐户。显示对话框后,我的 LogCat 中出现以下错误。请注意,这是他们 select 之前的帐户。

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}

一旦用户 select 注册了帐户,就会调用 OnActivityResult 并且 resultCode 始终为 "Canceled",这应该表示用户关闭了对话框,但这肯定不是这里发生的情况。有什么帮助吗?开发人员控制台中似乎有问题,但在按照指南浏览 100 次并得到相同结果后,我开始发疯了。

所以我的问题是我使用了错误的 debug.keystore。我的 Mac 同时安装了 Android Studio 和 Xamarin Studio。我错误地认为 Xamarin 使用的是“~/.android/debug.keystore”,但事实证明他们将它们放在“~/.local/share/Xamarin/Mono for Android/debug.keystore”中,改为使用来自此密钥的 SHA1 解决了我的问题。有关 Xamarin 密钥的信息: https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/MD5_SHA1/#OSX

使用终端为 Xamarin 获取正确的 SHA 使用:

keytool -list -v -keystore ~/.local/share/Xamarin/Mono\ for\ Android/debug.keystore -alias androiddebugkey -storepass android -keypass android

正如@thedigitalsean 所指出的,Android Studio 和 Xamarin (Visual Studio) 有不同的密钥库。

Android Studio Keystore 位于 .android

Xamarin 密钥库位于 .local/share/Xamarin/Mono 位置 Android

微软参考:https://docs.microsoft.com/en-us/xamarin/android/deploy-test/signing/keystore-signature?tabs=vsmac