【发布时间】: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 密钥中? stackoverflow.com/questions/30650231/…第二个分析器
标签: android xamarin google-api xamarin.android google-fit