Google API客户端OAuth始终返回已取消

时间:2016-02-26 21:16:44

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

我正在尝试使用Google Fit History API,我遇到了一个问题,在我使用ConnectionResult.StartResolutionForResult提示用户使用他们的Google帐户之后,即使用户选择了,我总是会收到取消代码CANCELED帐户通过对话框。据我所知,我已经按照这里的指南(https://developers.google.com/fit/android/get-api-key)来写信了。我在Developers控制台中有一个项目。我在控制台中启用了Fitness API。我在开发机器上使用调试密钥库生成了一个客户端ID。以下是开发者控制台的一些截图: Screenshot of API enabled in developer console Screenshot of OAuth Client ID in developer console

我正在使用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次之后会有相同的结果,我开始变得疯狂。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

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

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

正如@thedigitalsean所指出的,Android Studio& amp; Xamarin(Visual Studio)。

Android Studio Keystore位于 .android

位置

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

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