【问题标题】:Google API Client OAuth Always Returns CanceledGoogle API 客户端 OAuth 始终返回 Cancelled
【发布时间】:2016-02-26 21:16:43
【问题描述】:

我正在尝试使用 Google Fit History API,但遇到了一个问题,在我使用 ConnectionResult.StartResolutionForResult 提示用户输入他们的 Google 帐户后,即使用户选择了通过对话框帐户。据我所知,我已按照此处 (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 ();
                }
            }
        }
    }

使用 statusCode=SIGN_IN_REQUIRED 调用 OnFailedConnectionListener,然后我调用 StartResolutionForResult 并弹出对话框让用户选择他们的 Google 帐户。一旦显示对话框,我的 LogCat 中就会出现以下错误。请注意,这是在他们选择帐户之前。

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}

一旦用户选择了帐户,就会调用 OnActivityResult 并且 resultCode 总是“取消”,这应该表示用户关闭了对话框,但这肯定不是这里发生的情况。有什么帮助吗?在开发者控制台中闻起来好像有问题,但是在阅读指南 100 次并获得相同的结果后,我开始发疯了。

【问题讨论】:

标签: android xamarin google-api xamarin.android google-fit


【解决方案1】:

所以我的问题是我使用了错误的 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

【讨论】:

  • 您是否也更新了 Google 控制台中的 SHA1 密钥?我已经这样做了,但仍然遇到同样的问题。
【解决方案2】:

使用终端为 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 密钥库位于 .android

位置

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

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

【讨论】:

    猜你喜欢
    • 2016-04-10
    • 1970-01-01
    • 2013-11-07
    • 2020-05-24
    • 2021-02-18
    • 2012-10-31
    • 2014-06-18
    • 2020-01-10
    • 1970-01-01
    相关资源
    最近更新 更多